Search code examples
typescriptreduxredux-toolkitredux-thunk

Is it okay to use the same reducer to update one of two state properties depending on other state value?


So let's say hypothetically, I have one reducer where I want to either update state.propertyA OR state.propertyB depending on the value of state.propertyC. something like:

const conditionalReducer: CaseReducer<StateType, PayloadActionType> = (state, action) => {
    if(state.propertyC === true) {
        state.propertyA = action.payload;
    } else {
        state.propertyB = action.payload;
    }
}

Is this considered good practice, or should I instead make two separate reducers and apply the conditional logic before deciding which action to run?

I have tried looking around for an answer on this but haven't found anything specific regarding this in particular. I am finding it a little hard to understand where to do what in the redux state flow and what is allowed, following best practice.

I am also using the thunk middleware elsewhere in the application, however I am pretty new to that but figured it could be worth to mention in case there is a better option including thunk!


Solution

  • This is perfectly fine. If you take a look at the Redux Style guide, you will find that you should put as much logic as possible into your Reducers