r/reactjs • u/aandi134 • Jul 02 '26
Needs Help Avoiding the double render when syncing local state with context?
Solved
I have a component that copies data from a global context into local useState so the user can make edits before saving.
When the context data changes (e.g., switching pages), the local state doesn't update because it already initialized. If I use useEffect to watch the context and reset the state, it causes a visible double render where the old data flashes for a frame.
Is using key={id} to force a remount the only real fix here, or is there a better architectural pattern?
13
Upvotes
13
u/ferrybig Jul 02 '26
Use a key if you want to reset all state: https://react.dev/learn/you-might-not-need-an-effect#resetting-all-state-when-a-prop-changes
Use an
ifif you want to reset some state: https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes4
u/Thisisvexx Jul 02 '26
use a page-wide context provider and useReducer hooks I'd say
Or use a global state lib like zustand or jotai
4
u/Dozla78 Jul 02 '26 edited Jul 02 '26
I haven't used react in a while but this is what global state libraries are good at. You can use something like redux, mobx etc.
If you want to implement it yourself I'd use a reducer that emits an event after updating the global state and listen to it wherever I want
Edit: I've just remembered reducers in react should be pure filunctions(As I said I haven't done react in a while). Do not emit the event in there, my guess is you could use a useffect to emit it but you would cause an extra render on the child component
3
u/Mindless-Arrival-106 Jul 02 '26
I spent way too long trying to "eliminate" these renders before realizing I was optimizing the wrong thing.
If you're syncing props into local state, ask why that state exists in the first place. Half the time it doesn't need to.
1
u/yksvaan Jul 02 '26
Get rid of context and move the data to proper store.
1
u/jakiestfu Jul 02 '26
You’re likely going to have 2 renders because you’re setting react state twice. Maybe consider enriching your context state management with local storage, or consider finding a way to be OK with that 2nd render. You can certainly mitigate its impact on child components
1
u/AndrewGreenh Jul 03 '26
Change the state model.
The local component should just keep a „changes“ object.
The component then always merges context with changes.
When context changes, it will always directly appear in the UI, EXCEPT if it’s a field the user already changed, which is absolutely correct in my opinion.
User changes should never automatically change.
Them once you saved, you just clear the changes state.
1
u/alejandrodeveloper Jul 03 '26
if the local state is basically a “draft” of the context data,
key={id}is honestly one of the cleanest fixes. The bigger rule is usually if you find yourself copying props/context into local state and constantly syncing it, that’s often a sign the state boundary might be in the wrong place1
u/ruindd Jul 03 '26
Why use useEffect to watch the context?
Maybe I’m missing something but it’s my understanding that if a context changes it’ll re-render everything that’s a child element of the contextProvider.
1
u/[deleted] Jul 02 '26
[removed] — view removed comment