-2

Just trying to figure out the best practice in using useReducer + context hooks.

Which is better practice?

  1. Have one useReducer in the provider. ( 1 giant initial state + multiple combined reducers )
  2. Have multiple useReducers in the provider each managing their own state

1 Answers1

0

I prefer to use one reducer per provider. If there are multiple cases where I need global state I usually create multiple providers. (e.g. a provider for notifications and one provider for a todo list or something like that. Usually their domain is decoupled and components don't need to access both providers)

For each global state I usually also create two providers in itself. One for the actual state from the reducer and one for the dispatch. This way components who simply want to trigger an action won't update when the state changes. Although that's actually an unnecessary optimiziation for most react applications.

Bitmonk
  • 61
  • 3