Mutation in provider
I'd like to save some database bandwidth so I decided to debounced the save.
I'm passing the localstorage version of my data to children of provider and mutate on debounced value after 3 seconds.
The problem is after 3 seconds the mutation is triggered and cause a rerender of my provider and all the child components, which is an huge performance issue.
Maybe I'm doing it the wrong way but I'm looking for a solution.
PS : I don't need mutation result.
2 Replies
compatible-crimsonOP•3y ago
up ?
flat-fuchsia•3y ago
I'd focus on trying to remove the useStates within this hook since they cause re-renders (shouldn't have to use extra state for loading indicators, react-query should manage that for you).
Also look at what makes the value passed to the Context provider change: for example
handleUpdateUserTraining has a new reference at each render, which causes all the context consumers to re-render every time.
Finally, for debouncing, I would debounce the mutation function itself (using a useDebounce hook or similar) rather than relying on the userTrainingLocal state + useEffect, which is an antipattern: https://react.dev/learn/you-might-not-need-an-effect#sending-a-post-request. Using that guide, I would look into the other useEffects and see if they can be removed as well.