r/reactjs • u/Temperature_Majestic • Aug 18 '26
Discussion Why do sibling components re-render even when their own props didn't change?
Ran into this explaining React rendering to someone recently and realized how often it trips people up even after they've been writing React a while.
function Parent() {
const [count, setCount] = useState(0);
return (
<>
<button onClick={() => setCount(c => c + 1)}>{count}</button>
<ExpensiveChild />
</>
);
}ExpensiveChild takes no props at all. Click the button and it re-renders anyway, every single time. No props changed, nothing it reads changed, it just runs again.
The reason: React doesn't check "did this component's inputs change" before deciding to re-render. When state updates, React re-renders that component and everything below it in the tree by default, full stop. Whether a child actually needed to update isn't part of that decision at all.
React.memo is what actually opts a component into that check, it wraps the component and does a shallow prop comparison before deciding to skip the render. Without it, "no props" and "props didn't change" both mean nothing, React re-runs the function anyway.
Where it gets messier: memo alone doesn't save you if you're passing an inline function or object as a prop, since those are new references every render and memo's shallow comparison sees them as "changed" regardless. You end up needing useCallback/useMemo on the parent side just to make memo's comparison actually mean something.
Curious how many people actually reach for memo proactively vs only after profiling shows a real problem. What's the actual signal that told you a component needed it?
74
u/kevin074 Aug 18 '26
what's being re-rendered isn't the sibling, but the parent. Your click function triggers set state on a state that is created and kept record by the parent.
to prevent ExpensiveChild from re-rendering, use React.memo; not useMemo, that's for the props.
19
u/CardinalHijack Aug 18 '26
By default React parent components also re-renders their children. Here setCount will cause a re-render.
It doesn't matter that ExpensiveChild has no props (or that its props haven't changed). A parent re-render causes its child components to be evaluated again.
As others have mentioned,
React.memo(ExpensiveChild)can prevent the child from re-rendering when its props haven't changed.7
u/Throwaway_0815_123 Aug 18 '26
One of the downsides of lifting state up.
10
u/journeypiggyman Aug 18 '26
OP is AI?
1
u/Hobby101 Aug 18 '26
button would need to rerender even with memo, because it's child, i.e. label that is counter value is changing
1
u/TSpoon3000 Aug 18 '26
You were already using React Compiler here or not?
1
u/r-nck-51 Aug 18 '26 edited Aug 18 '26
Parents render their children in React.
Not to confuse with
return (<>{children}</>)where you pass a rendered component to the parent from app or the "grandparent", as opposed to passing a functional component reference in your example.Looking at your generic example I would say: move your state to the lowest nested component if no other sibling needs it...
But the solution really depends on the use case, sometimes a context helps, a React.memo, useCallback, useMemo, and sometimes there are libraries that can help. But it all depends...
https://react.dev/reference/react/useContext#optimizing-re-renders-when-passing-objects-and-functions
1
u/youakeem Aug 18 '26
Components by default rerender in only two cases: 1) when their state changes
2) when their parents rerender
Props have nothing to do with rerendering. If you want a component to not render when its parent does, use React.memo.
1
u/bigabig Aug 18 '26
I haven't dealt with react compiler yet, but would this be solved automatically by it?
1
u/92smola Aug 18 '26
The way I look at it its a function that reruns when its state changes, it can be nested in another function which can also re run and by that re run it as a an inner finction, I mean its literally that, if you drop the rerender and component naming its easier to reason about it
1
u/Civil_Cheesecake2492 Aug 18 '26
Think of composition as a tool to prevent unnecessary rerenders. Expensive component can be passed in either as children or a prop
1
u/hyrumwhite Aug 19 '26
The only way for react to know if state has changed is to re-run render functions. If parent state changes in any way, every child needs to rerender to know if it’s been affected.
Memos are a way to say, “I only care about this items in particular” and dodge the rerender.
1
u/Vincent_CWS Aug 19 '26
can try react compiler
1
u/[deleted] Aug 20 '26
If any library of framework is shipped with "tools" like memo, then you should know it is not good architecture, but a means to apply band-aids over inefficient re-rendering models instead of solving the core reactivity issue at the root.
0
u/christfrost Aug 18 '26
Not sure what to tell you, I guess the framework name "react" is pretty evident. That's how it is designed to behave. Entire tree gets an update if parent updates. That's ReactJS 101.
memo or react-compiler is meant to solve such problems.
Edit: Not specifically in this example, but having an inline anonymous functions as callbacks are going to create similar-ish problems for you as you're creating a fresh new reference each time you click it, so that ultimately informs react to do a check and decide whether it should re-render its tree (yes, it will in this case whether you want it or not).
0
u/martoxdlol Aug 18 '26
I used to not care about that until I failed a job interview. I didn't fail due to the react question alone but it did make go and learn about it. Also, react compiler is a nice cheat to automatically deal with this. React could have smarter designs by default, for example they could add some later to remap function props in a way that doesn't force a rerender.
0
u/Noch_ein_Kamel Aug 18 '26
Everything was better with class inheritance and shouldComponentUpdate() ;P
-1
u/bluespacecolombo Aug 18 '26
I’ve been writing React almost since its inception so I might be biased, but idk who gets tripped by this? This is like the most basic behavior of react and the post itself feels like karma farming. You learn this probably in the few first pages in the docs
1
u/[deleted] Aug 19 '26
[removed] — view removed comment