Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

provide original store slice state before in went through Tesler reducer as a reducer argument to allow overriding built-in behavior #588

Merged
merged 1 commit into from
Feb 18, 2021

Conversation

Dergash
Copy link
Member

@Dergash Dergash commented Feb 3, 2021

At the moment there is no obvious way to override Tesler reducer reacting to built-in action:

export interface ClientReducer<ReducerState, ClientActions> {
initialState: ReducerState
override?: boolean

ClientReducer type provides override flag and it is in fact respected, but it is conceptionally flawed in a way that most projects treat slice reducer as a single function with a giant switch inside, so this flag will replace reducers for the entire store slice.
This has absolutely no usage in real life and will be removed 2.0.0

Two other currently available options:

  1. Use custom action type - not always convinient as Tesler action might be dispatched deeply from Tesler epic or component so overriding reducer for one action will require a large chunk of code from client application developer; not vialable
  2. Revert changes from Tesler reducer manually in client side reducer; this is how it's currently handled in most projects. Some disadvantages: developer has to look into Tesler implementation to understand what to revert; changes in Tesler reducer will break client reducer; addional code.

This PR provides client application reducer with additional argument: originalState, that contains the state of Redux store slice before it went through Tesler reducer:

export function session(
  state = initialState,
  action: AnyAction,
  store: Store,
  originalState: Session
): Session {
  switch (action.type) {
    case types.loginDone: {
      // implement custom logic and return based on `originalState` instead of `state`
      return { ...originalState }
    }
    // ...
  }
}

@Dergash Dergash added documentation Improvements or additions to documentation enhancement New feature or request tests Unit tests and coverage labels Feb 3, 2021
@Dergash Dergash requested a review from ai-ba February 3, 2021 13:30
…cer as a reducer argument to allow overriding built-in behavior
@Dergash Dergash force-pushed the feat/reducer-original-state branch from 910414b to ee02478 Compare February 3, 2021 13:58
@Dergash Dergash merged commit f0c1b08 into develop Feb 18, 2021
@Dergash Dergash deleted the feat/reducer-original-state branch February 18, 2021 09:51
@Dergash Dergash added this to the 1.28.0 milestone Feb 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation enhancement New feature or request tests Unit tests and coverage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants