Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading