Something new I learned about React today… I thought setState() updates immediately. But React batches state updates. Example: 👉 setCount(count + 1); 👉 setCount(count + 1); You might expect +2… But it becomes +1. Why? React queues updates and processes them together in the next render cycle. Both calls use the same previous count value. Why does React do this? Because DOM updates are expensive. Batching helps: ✅ Reduce unnecessary re-renders ✅ Improve performance ✅ Keep UI updates efficient Correct way when depending on previous state: 👉 setCount(prev => prev + 1); 👉 setCount(prev => prev + 1); 💡 Realization: State updates are scheduled, not instant. #ReactJS #JavaScript #FrontendDevelopment #WebDevelopment #SoftwareEngineering #CodingJourney #LearningInPublic #DeveloperLife #ReactInternals #FrontendEngineer #TechInterview #StateManagement
React State Updates Batching Explained
More Relevant Posts
-
🚀 Understanding Reconciliation in React.js One of the core concepts that makes React powerful and efficient is Reconciliation. When state or props change, React doesn’t blindly update the entire DOM. Instead, it: 1️⃣ Creates a new Virtual DOM 2️⃣ Compares it with the previous Virtual DOM (Diffing) 3️⃣ Identifies what actually changed 4️⃣ Updates only those specific parts in the Browser DOM This process is called Reconciliation. 💡 Why is this important? Because updating the real DOM is expensive. By updating only the changed elements, React ensures high performance and smooth UI updates. 🔑 Key takeaway: Same element type → Update only changed props Different element type → Destroy and rebuild Lists require proper keys for efficient diffing Understanding reconciliation helps you write better, optimized React applications. #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #VirtualDOM #ReactDeveloper #SoftwareEngineering #CodingJourney #TechLearning
To view or add a comment, sign in
-
-
⚛️ React Batching — Why It Matters React doesn’t re-render on every setState. It batches multiple state updates into a single render for better performance. setCount(c => c + 1); setFlag(f => !f); 👉 2 updates 👉 1 render 🔥 React 17 vs React 18 React 17 Batching worked only inside React event handlers. Not inside setTimeout, promises, or async calls. React 18 Introduced Automatic Batching — works everywhere ✔ Events ✔ setTimeout ✔ Promises ✔ async/await (When using createRoot) 🧠 Why This Is Important Fewer re-renders Better performance Reduced reconciliation work Smoother UI ⚠️ Need Immediate Update? Use flushSync() — but carefully. 💡 Interview Insight: Functional updates (setState(prev => ...)) are safer because batching may group updates and functional updates ensure you're working with the latest state. Small concept. Big performance impact. 🚀 #ReactJS #FrontendEngineering #JavaScript #React18 #WebPerformance
To view or add a comment, sign in
-
Migrating a large legacy frontend to React sounds easy on paper but in reality, it isn’t. The codebase had years of Dojo modules. All running in production and used by real users every day. A big rewrite was not an option as its too risky. So we did it the slow way. One module at a time. - Understand it. - Rewrite it in React. - Ship it. - Move to the next one. For a long time both Dojo and React were running together in production. That part was tricky as everything had to work exactly the same for users. We treated every migrated module like a new feature with full testing. After 10+ modules the results started showing up. -> Pages loaded about 30% faster. -> Bundle size dropped by around 35%. -> And the codebase became much easier to work with. While the migration was ongoing, the product never went down. Sometimes the boring, incremental approach is the right one. #frontend #reactjs #webdevelopment #javascript #softwareengineering
To view or add a comment, sign in
-
Why can’t we create a state variable outside the component function? const [count, setCount] = React.useState(0); ❌ function Counter() { return <div>{count}</div>; } function Counter() { const [count, setCount] = React.useState(0); ✅ return <div>{count}</div>; } Why? Because Hooks rely on the component’s render cycle. When React renders a component, it: 1️⃣ Calls the component function 2️⃣ Tracks the order of Hooks 3️⃣ Stores state internally based on that order Hooks are tightly connected to the component’s lifecycle. If you declare state outside: ❌ There is no render context ❌ No component lifecycle ❌ React can’t track it #ReactJS #ReactHooks #ReactInternals #FrontendDevelopment #JavaScript #SoftwareEngineering #WebDevelopment #LearningInPublic #ReactInterview #FrontendEngineer #TechInterview
To view or add a comment, sign in
-
Today I learned an important concept in React about the useState hook. Some key points that helped me understand it better: setState (or the set function) does three things: It updates the state value. It triggers the component to re-render. React batches multiple updates together for better performance. Another interesting thing is that if the previous value is equal to the new value, React does not re-render the component. Understanding how state updates and batching work helps in writing more efficient React components. Devendra Dhote Ritik Rajput Dhanesh Parwati Malviya Sarthak Sharma #React #JavaScript #WebDevelopment #FrontendDevelopment
To view or add a comment, sign in
-
-
Today I learned something interesting about React's useState. When we call multiple state updates like: setCount(count + 1) setText("Updated") React doesn’t re-render the component multiple times. Instead, React batches these updates together and performs only one re-render. This improves performance and keeps the UI efficient. Understanding these small things happening behind the scenes really helps in writing better React code. #React #JavaScript #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
💻 Revisiting React Fundamentals – Understanding State Today I revisited one of the core concepts of React: State management using the useState hook. To reinforce the concept, I built a small Counter component that performs different operations such as: ✔️ Increment ✔️ Decrement ✔️ Reset ✔️ Multiply by 2 ✔️ Square of a number This simple exercise helped me understand how state changes trigger UI updates in React and how event handlers like onClick interact with state. Sometimes revisiting basic concepts helps strengthen the foundation before building more complex applications. I’ve attached a short screen recording and code snippets showing how the counter works. Always learning, always improving. 🚀 #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #LearningInPublic #ReactDeveloper
To view or add a comment, sign in
-
useEffect is one of the most misunderstood hooks in React. Most bugs I’ve seen weren’t because React is complex — they were because the mental model was wrong. Here’s a practical breakdown with real examples. Day 2/100 — sharing practical frontend engineering lessons from real-world projects. What’s the most confusing useEffect bug you’ve faced? #ReactJS #FrontendEngineering #JavaScript #WebPerformance #SoftwareArchitecture
To view or add a comment, sign in
-
-
React Hooks: useState vs useRef — Know the Difference When working with React functional components, two commonly used hooks are useState and useRef. While they may seem similar at first, they serve different purposes. -- useState * Used to store and manage component state * When the state updates, React re-renders the component * Ideal for data that affects the UI Example: const [count, setCount] = useState(0); -- useRef * Used to store mutable values that persist across renders * Updating a ref does NOT trigger a re-render * Commonly used for accessing DOM elements or storing previous values Example: const inputRef = useRef(null); -- Simple Rule to Remember: * If the value affects the UI → useState * If the value should persist but not trigger re-render → useRef Mastering these hooks helps you write cleaner and more efficient React components. #ReactJS #WebDevelopment #FrontendDevelopment #JavaScript #ReactHooks #SoftwareDevelopment
To view or add a comment, sign in
-
Why Array Comparison in React State Is Tricky? If you’ve ever updated an array in React and your component didn’t re-render, you’re not alone. The reason? React compares state by reference, not by value. So if you modify the same array instead of creating a new one, React thinks nothing changed — even if the data inside it did. Never mutate state directly. Always create a new array or object when updating state. #ReactJS #JavaScript #FrontendDevelopment #WebDevelopment
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
I encountered this issue in the beginning as well. Over time I understood how writing the logic slightly differently can prevent it. Really well explained!