Skip to content

replaceReducer safely #2017

@davidnormo

Description

@davidnormo

I am thinking of using store.replaceReducer to swap reducers so that an action type can target a particular reducer. This pattern would help with reusing action types on different pages of an application.

For example:

  1. on /pageA we have reducer A and the following state tree: { A: { ... } }
  2. MY_ACTION is dispatched and handled by A and produces a new state A.
  3. User goes to /pageB, we replaces reducer A with reducer B and end up with { A: { ... }, B: { ... } }
  4. MY_ACTION is dispatched that B handles and updates state B but not state A.

The problem with the above approach is that Redux does not currently provide any safety while replacing a reducer. For example the following scenario is a problem:

fetch().then(dispatch(action));

store.replaceReducer(B);

// dispatch happens by might not have been intended for reducer B - a race condition!

Is there something I'm missing here? Or would you be willing to consider a PR to make this possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions