gaurav5430
gaurav5430

Reputation: 13917

React - multiple unrelated values in context provider

I have a settings page which allow the user to enable/disable various settings. Currently I am using a single settings context provider to pass on all the settings.

Would it actually be better to isolate all the settings in separate contexts, so that only the components which use a particular setting would rerender when that setting changes. In my current implementation it seems like even if a unrelated setting changes, the consumers would be rerendered.

Is it true even if they are not consuming the changed value?

Upvotes: 1

Views: 1625

Answers (1)

Ryan Cogswell
Ryan Cogswell

Reputation: 81106

As far as what React knows, you aren't consuming particular values from the context, you are just consuming a particular type of context. If the value (provided by the provider for that type of context) changes, all the consumers need to be re-rendered. If the value is an object and you only use one part of it, the context API doesn't currently provide any way for you to tell React which parts a particular component is using.

My answer here provides some guidance on deciding what to group together.

Upvotes: 2

Related Questions