Thomas Banderas
Thomas Banderas

Reputation: 1739

How to connect reducers logic

const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state /*do sth other */})),
  on(loginSuccessful, loginFailed, (state) => ({...state, isLogging: false}))
);

In example above I have shared logic between multiple actions (isLogging property). But only one of these reducers will be fired when loginSuccesfull action was dispatched.

It is possible to connect shared logic instead of writing:

const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state, isLogging: false /*do sth other */})),
  on(loginFailed, (state) => ({...state, isLogging: false}))
);

Upvotes: 1

Views: 39

Answers (1)

timdeschryver
timdeschryver

Reputation: 15505

This is not possible (for now), only the last registered action of the same action's type receives the update.

const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state /*do sth other */})),
  on(loginSuccessful, loginFailed, (state) => ({...state, isLogging: false})) // only this one will be triggered for loginSuccessful
);

We just merged a PR to make your use case possible, this will probably get "out" with the next release.

Upvotes: 2

Related Questions