Sign in
Topics
Generate full-stack React apps with figma or prompts.
Is your React component re-rendering too often? Learn how to prevent child component re-renders using simple React techniques like memo, useCallback, and smart prop management for better app performance.
Your React app updates only a small part of the UI, yet it still feels slow.
What’s causing this lag?
In many cases, frequent re-renders of a child component are the real issue. Learning how to prevent a child component from re-rendering in React can help you fix that slowdown. Also, it helps you write apps that feel snappier and stay easier to manage as they grow.
This article covers smart ways to stop extra renders using tools like useCallback , memo, and referential equality. You’ll see how props changes affect rendering and how to handle them with simple code updates.
Keep reading to find out what triggers re-renders and how to stop them without overcomplicating your code.
Use useCallback
to preserve function references across renders.
Memoize child components to avoid unnecessary renders.
Understand when props change triggers updates.
Debug with logs to track what is re-rendering and why.
Keep parent component clean to avoid cascading renders.
A child component re-renders when its props or state change. This happens even if the value of the prop is the same, but its reference is different. This is due to referential equality, which React uses to compare props between renders.
Let’s look at a simple example:
1const Parent = () => { 2 const handleClick = () => console.log("Clicked"); 3 4 return <ChildComponent onClick={handleClick} />; 5};
Here, the function handleClick
is recreated on every render of the parent component, causing the child component to re-render unnecessarily.
Defining a function inline leads to re-creating it on every render, breaking referential equality.
Passing a new object or array on each render makes React treat it as a new prop.
1<Child data={{ value: 1 }} />
This will cause re-renders, even if the actual data hasn’t changed.
The useCallback
hook helps preserve the same function reference between renders.
1const memoizedFunction = useCallback(fn, [dependencies]);
useCallback returns
the same function reference unless its dependency list changes.
This avoids re-creating functions and keeps child components from re-rendering.
1const Parent = () => { 2 const [count, setCount] = useState(0); 3 4 const handleClick = useCallback(() => { 5 console.log("Clicked"); 6 }, []); // Empty dependency list 7 8 return <ChildComponent onClick={handleClick} />; 9};
With this change, handleClick
is memoized, and the child component does not re-render when parent's count
changes.
Wrap your child component in React.memo
and use useCallback
to prevent unwanted renders.
1const ChildComponent = React.memo(({ onClick }) => { 2 console.log("Child rendered"); 3 return <button onClick={onClick}>Click</button>; 4});
When used together, this pattern reduces unnecessary component re-renders dramatically.
React uses referential equality to determine if a prop has changed.
1{} === {} // false 2[] === [] // false
Even if the content is the same, a new object or array causes a false negative, triggering re-renders.
To avoid this, use useMemo or useCallback
to cache the value.
1const Child = React.memo(function Child({ onClick }) { 2 console.log("Rendering child"); 3 return <button onClick={onClick}>Child</button>; 4});
1const Parent = () => { 2 const [count, setCount] = useState(0); 3 4 const handleChildClick = useCallback(() => { 5 console.log("Child clicked"); 6 }, []); 7 8 return ( 9 <div> 10 <Child onClick={handleChildClick} /> 11 <button onClick={() => setCount(count + 1)}>Increase Count</button> 12 </div> 13 ); 14};
Now the child doesn’t re-render when parent’s count
updates.
"Using useCallback can drastically reduce unnecessary child component re renders by keeping function references stable. This simple adjustment helps maintain predictable render cycles and better overall performance." - LinkedIn
Here’s a diagram showing what causes re-renders.
If the function or object reference changes, the child component re-renders. useCallback and memo
help stop that chain.
Use console.log inside components to check what’s rendering and why.
1const Child = React.memo(({ onClick }) => { 2 console.log("Child rendered"); 3 return <button onClick={onClick}>Child</button>; 4});
Track how often this log appears to measure performance.
Avoid re-creating arrays or objects on every render by using useMemo
.
1const data = useMemo(() => ({ count }), [count]);
This keeps the same object reference unless the count changes.
Problem | Fix |
---|---|
Inline functions | useCallback |
New object or array props | useMemo |
Child not memoized | React.memo |
Too many parent renders | Split components |
Unknown re-renders | Use console.log to debug |
Extra renders slow down your React app and make debugging harder. This often happens when you pass new inline functions or unstable props to child components.
We looked at how tools like useCallback, memo, and proper dependency handling help avoid these problems. They keep your components from re-rendering when nothing has changed.
As your parent components grow, small inefficiencies start adding up. So it’s better to address them early.
Try these techniques in your code. Use logs to track render behavior. And keep your app fast and easy to maintain.