most developers don't know the difference between null , undefined and "" and it's breaking their React forms silently. - always initialise string state with ' ' not undefined - always initialise array state with [ ] not undefined - always initialise object state with { } not undefined here's why it matters beyond the warning: - undefined means "this was never set" - null means "this was intentionally set to nothing" - ' ' means "this exists but is currently empty" React treats these three things completely differently when rendering. your form works locally because you fill it in immediately. it breaks in production because someone submits without touching a field. initialise your state properly. #reactjs #typescript #webdevelopment #buildinpublic #javascript
Krish .’s Post
More Relevant Posts
-
Hey JavaScript devs 👋 Did you know `this` can silently disappear in 3 very common situations? 1. Detaching a method from its object const fn = obj.greet; fn(); // this → undefined 💀 2. Passing a method as a callback setTimeout(obj.greet, 0); // this → gone [1,2,3].forEach(obj.process); // same story 3. Arrow function in an object literal const obj = { name: 'Pavel', greet: () => console.log(this.name) // this → global, not obj }; The fix is always one of three things: `.bind()`, a wrapper arrow function, or class field arrow methods. Which one bit you the hardest? Drop it in the comments 👇 #JavaScript #WebDev #Frontend #JS
To view or add a comment, sign in
-
-
One React topic that gets messy fast: forms. Not because inputs are hard. Because real-world forms are. Validation, async errors, dynamic fields, reusable logic: that’s where clean React patterns matter. I broke it down here 👇 https://lnkd.in/dFWB3Phc #reactjs #frontenddevelopment #javascript #webdevelopment #softwareengineering #buildinpublic
To view or add a comment, sign in
-
-
🚀 Using `match()` Method of String Object (JavaScript) The `match()` method of a string object searches the string for a match against a regular expression. If the `g` flag is present, it returns an array of all matching substrings. If the `g` flag is absent, it returns the same type of array as `exec()`. If no match is found, it returns `null`. It's a versatile method for extracting matching substrings from a string. #JavaScript #WebDev #Frontend #JS #professional #career #development
To view or add a comment, sign in
-
-
The Great Debate: Formik or React Hook Form? Both are industry favorites, but they offer two very different approaches to handling forms in React. While one is praised for its stability and ease of use, the other is the go-to for performance and minimal re-renders. Most developers have a strong preference, but is there really a "winner"? Which one is currently in your tech stack, and why did you choose it over the other? 👇 #ReactJS #FrontendDevelopment #WebPerformance #ProgrammingTips #Javascript
To view or add a comment, sign in
-
💡 #JavaScript Global vs Local Variables (Simple Explanation) If you're learning JavaScript, understanding variable scope is a must 👇 🔹 Global Variables Declared outside any function Accessible from anywhere in your code Can be used across multiple functions Example: var name = "Avi"; function greet() { console.log(name); // Accessible here } 🔹 Local Variables Declared inside a function or block Accessible only within that function/block Helps avoid unwanted changes from outside Example: function greet() { var message = "Hello"; console.log(message); // Works here } console.log(message); // ❌ Error ⚡ Key Difference Global = accessible everywhere Local = accessible only inside its scope 👉 Tip: Prefer #local variables to keep your code clean and avoid bugs. Use #global where multiple parts of your app need the same value. #frontend #js #javascript
To view or add a comment, sign in
-
-
Quick JavaScript Question for Developers Do you think this is true or false? [] == ![] I actually came across this while working on a feature. At one point, my condition was behaving in a way I didn’t expect… and this was the reason behind it. 👉 The result is: true Why is it true? It looks like a small thing…But the impact? [] is truthy → so ![] becomes false Now it becomes: [] == false JavaScript then converts both sides to numbers: false → 0 [] → "" → 0 So finally: 0 == 0 → true Always prefer === to avoid these surprises #javascript #coding #webdevelopment #developers #js
To view or add a comment, sign in
-
-
💡 𝗧𝗶𝗽 𝗼𝗳 𝘁𝗵𝗲 𝗗𝗮𝘆 — 𝗥𝗲𝗮𝗰𝘁 𝗗𝗶𝗱 𝘆𝗼𝘂 𝗸𝗻𝗼𝘄? In React, 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗱𝗲𝗳𝗶𝗻𝗲𝗱 𝗶𝗻𝘀𝗶𝗱𝗲 𝗰𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁𝘀 𝗮𝗿𝗲 𝗿𝗲𝗰𝗿𝗲𝗮𝘁𝗲𝗱 𝗼𝗻 𝗲𝘃𝗲𝗿𝘆 𝗿𝗲𝗻𝗱𝗲𝗿. This means passing them as props can trigger unnecessary re-renders in child components. 🔧 𝗕𝗲𝘀𝘁 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗲: - Use "useCallback" to memoize functions when passing them down - Only do this when necessary (e.g., with "React.memo" or dependency arrays) Not every function needs memoization—but knowing when it matters is key. #React #ReactJS #FrontendDevelopment #JavaScript #WebDevelopment #PerformanceOptimization #SoftwareEngineering #CodingTips #FullstackDeveloper
To view or add a comment, sign in
-
-
What is a closure in JavaScript? A closure is a function that remembers variables from its outer scope even after that scope has finished executing. Why does this work? - `createCounter` runs once - It creates a variable `count` - The inner function “closes over” that variable - Even after `createCounter` finishes, `count` is still accessible Each time `counter()` runs: → it uses the same preserved state 💡 Closures are everywhere: - React hooks - Event handlers - Memoization - Encapsulation patterns They’re not just a concept — they’re part of how JavaScript manages state. #Frontend #JavaScript #React #WebDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
-
React 19.2 introduces the <Activity> component, which eliminates the issue of losing form state when switching tabs. This update encourages developers to stop unmounting components, offering a new approach to handle state effectively. #ReactJS #Javascript #FrontendDeveloper #React19 #WebPerformance #CodingTips #SoftwareArchitecture #StateManagement #JS #ProgrammingLife
To view or add a comment, sign in
-
you called setState twice. expected +2. got +1. both calls read the same stale count from the current render. they don't compound. they both say "set to 1." welcome to React. why it breaks: state updates don't happen immediately. they're scheduled. so count is still 0 inside your function no matter how many times you call setState. both calls read 0. both set to 1. result: 1. the Fix: use functional updates. each call gets the previous result and builds on it. The code : #reactjs #javascript #webdevelopment #buildinpublic #typescript
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Well, you need validation on inputs to begin with. That will solve the problem you are discussing. Also, you have to hope that your build uses something like husky and lint-staged and that warnings are set to 0 and this forces a dev to fix any errors and warnings before being able to commit anything. For me, I don't care if a value is null, undefined. I'd rather that than an empty string. You can do specific checks for null and undefined. Using Typescript can be especially helpful. If you have a check like if (email){..} and it's an empty string, the if block will run. but if it's null or undefined, it won't. Me, I prefer to use the action prop on a form, With JS validation for the form, and only enable the submit button if form validation passes. This is flexible and can be implemented easily.