DEV Community

Discussion on: React Context+Hooks API=> Ideal State Management

Collapse
 
amitneuhaus profile image
Amit Neuhaus

I've heard context has some performance issue when there are alot of changes, aka the app is very dynamic and In those cases it is better to use redux, is that still the case or there were any changes?

Collapse
 
markohologram profile image
Marko A

As far as I know, problem with React Context is that if you update the values that you send to Provider, the whole React component tree that is wrapped in that Provider will re-render. So using it for app wide state might present some performance issues, depending on what are you doing.

Maybe using it for theme data won't be a problem since that's something that probably won't change that often. For some more dynamic state, maybe it's better to create multiple React Contexts that only provide data to smaller parts of the app or use a dedicated state library like Redux or MobX or many others that exist.

Collapse
 
amitneuhaus profile image
Amit Neuhaus

Got it,
Thank you, Great article.

Collapse
 
trangcongthanh profile image
Thành Trang

No. Only components wrapped inside Consumer will re-render or components with React.useContext to consum the data from Context will re-render.

Thread Thread
 
saswatamcode profile image
Saswata Mukherjee • Edited

Yup! Only components which subscribe to the Context changes, i.e, Consumers will re-render. However, Context should only be used when you require a few props to be available to a ton of components in a component tree, and prop drilling becomes cumbersome.

Thread Thread
 
markohologram profile image
Marko A

Yeah you are both right, my mistake...But still, the component will re-render even if the values that it's using from that Context instance don't change.

Still, it might be better to use multiple Context instances for multiple parts of the app, instead of having single Context provide bunch of values to many many components (eg. wrapping your whole app inside a single Context provider)