🚀 React Re-renders Why does a component re-render even when props look the same? React re-renders when reference changes, not value. If you pass new objects/functions every render, React treats them as new. Example: Passing `{ user }` where `user` is recreated each render will trigger child updates. In dashboards with 100+ rows, this causes lag. Fix: Memoize values and avoid inline object creation. #ReactJS #FrontendEngineering #JavaScript
React Re-renders: Why Reference Changes Matter
More Relevant Posts
-
Have you been there? You update your state, but your useEffect or useCallback or setInterval is still clinging to a value from three renders ago. It’s a Stale Closure. I put together a quick carousel to break down why this happens and what the possible solutions are. Understanding closures in terms of react rendering was the "aha!" moment which moved me from just making things work to actually understanding the why's. Let me know your thoughts in comments! #reactjs #javascript #closure #codingtips #winyourlinkedin
To view or add a comment, sign in
-
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
To view or add a comment, sign in
-
Today I learned how useEffect cleanup actually works in React — and it completely changed how I think about side effects. When we use useEffect, it runs after render. But what many beginners ignore is the cleanup function. Why is cleanup important? Prevents memory leaks Stops unnecessary API calls Removes event listeners properly Clears intervals and timeouts Example: When you add an event listener inside useEffect, you must remove it when the component unmounts. Otherwise, it keeps running in the background. That’s where cleanup comes in. useEffect(() => { const handleResize = () => console.log("Resized"); window.addEventListener("resize", handleResize); return () => { window.removeEventListener("resize", handleResize); }; }, []); The function returned inside useEffect runs: Before the next effect runs When the component unmounts Small detail. Big difference in performance. React is simple — until you understand the details. #ReactJS #FrontendDeveloper #JavaScript #WebDevelopment #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
Another core concept in React is props, short for “properties.” Props are how data moves from one component to another. Think of them as inputs you pass into a component so it can display or use that data. For example, you might have a reusable component that displays a user card. Instead of hardcoding the name or email inside the component, you pass that information in as props. The component stays flexible, and you can reuse it in different places with different data. This pattern is what makes React applications easier to organize. Small components receive data through props, render what they need, and stay focused on a single job. Once you start thinking in terms of components passing data through props, building larger interfaces becomes much more manageable. #reactjs #webdevelopment #frontenddevelopment #javascript #softwaredevelopment
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
-
⚛️ Understanding the Order of Hooks in React One important rule while working with React Hooks is that the order of hooks must remain the same on every render. React relies on the call order of hooks to correctly associate state and effects with a component. Example: function Example() { const [count, setCount] = useState(0); const [name, setName] = useState(""); useEffect(() => { console.log("Component mounted or updated"); }, []); return <div>{count}</div>; } 🔹 Hooks are executed top to bottom on every render. 🔹 Changing their order can break React’s internal state tracking. ❌ Incorrect usage: if (condition) { const [count, setCount] = useState(0); // ❌ Hooks inside condition } ✅ Correct approach: const [count, setCount] = useState(0); if (condition) { // logic here } 📌 Rules of Hooks 1️⃣ Call hooks only at the top level 2️⃣ Call hooks only inside React functions Understanding this helps avoid bugs and keeps components predictable. #React #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #Hooks
To view or add a comment, sign in
-
Today something interesting happened while I was building a Custom Date Picker in NextJS.🗓 I have used useRef many times before, but honestly I was mostly using it without fully understanding the concept behind it But today… it finally clicked.💫 While implementing the feature, I realized that: • useRef creates a mutable object • The value is stored inside .current • The value persists between renders • Updating .current does not trigger a re-render • It can also be used to directly access DOM elements The most interesting part for me was understanding why React doesn't re-render when current changes. Sometimes you don’t truly understand a concept until you build something real with it. Small learning today, but a very satisfying one. 💫 What was the React concept that took you the longest to understand❓️ #React #JavaScript #FrontendDevelopment #ReactHooks #BuildInPublic #LearningJourney
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
-
Most performance issues come down to one thing: Unnecessary Re-renders. React is fast, but it’s not magic. If you don't manage how and when your components update, your DOM will pay the price. In this carousel, I break down: The 4 core triggers of a re-render. The 'Waterfall Effect' in parent-child relationships. How to stabilize your code using professional patterns." #ReactJS #WebDevelopment #JavaScript #FrontendPerformance #MERNStack #ReactHooks #ZhairAhmad #SoftwareEngineering
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