I am writing some reducers with Redux-Sagas, Im quite new to this and all reducers we have now dont have any thing like I am about to do. Started to question myself if its the right thing to do.
In this case I have an INITIAL_STATE thats contains a dataProp, this data has an prop that is an array of users. Now I create a editUser form, when the user is updated I get the updated user back. to update that user in the ReactView I do like this:
const updateUser = (users, user) => {
//Update user and return array
};
[actions.updateUserSuccess]: (state, payload) => ({
...state,
viewData: {
...state.viewData,
users: updateUser(...state.viewData.Users, payload)
},
loadingUser: false,
}),
And the question, is it ok to put a method to handle a thing like this on top of the reducer file?
Sure, that's totally fine, and in fact we have a docs page on "SplittingReducer Logic" that even talks about that.
That said, you really should be using our official Redux Toolkit package to write your Redux logic. It will greatly simplify all your Redux code. In particular, it uses Immer to let you write "mutating" syntax in your reducers, and turns that into safe and correct immutable updates. No more object spreads!