Skip to content

Replacing state with a different slice does not work #4542

Closed Answered by EskiMojo14
chris-dem asked this question in Q&A
Discussion options

You must be logged in to vote

it's called initialState for a reason - it's only used when the reducer is called with undefined. In this case, we already have state for this reducer, so it's using that state instead and returning it.

let reducer1 = (state = { id: 1 }, action) => state;

let reducer2 = (state = { id: 2 }, action) => state;

let state = reducer1(undefined, { type: "INIT" });

state = reducer2(state, { type: "REPLACE" });

// state is still { id: 1 }

Replies: 1 comment 11 replies

Comment options

You must be logged in to vote
11 replies
@chris-dem
Comment options

@EskiMojo14
Comment options

@chris-dem
Comment options

@EskiMojo14
Comment options

Answer selected by chris-dem
@chris-dem
Comment options

@EskiMojo14
Comment options

@chris-dem
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants