Stateless reducers
yhnavein opened this issue · 1 comments
yhnavein commented
Hi,
I noticed in this repository reducers being written in this way:
reducers: {
getRepoDetailsSuccess(state, action: PayloadAction<RepoDetails>) {
state.openIssuesCount = action.payload.open_issues_count
state.error = null
},
}
Isn't it wrong? It looks like the reducer is changing an existing state, which must not happen.
IMHO reducers should look like
reducers: {
getRepoDetailsSuccess(state, action: PayloadAction<RepoDetails>) {
return {
...state,
openIssuesCount: action.payload.open_issues_count,
error: null
};
},
}
Am I missing something?
markerikson commented
Yes. As discussed in the tutorials and documentation, Redux Toolkit uses Immer inside, which allows you to write "mutating" code that is actually turned into correct immutable updates inside:
https://redux-toolkit.js.org/tutorials/intermediate-tutorial#mutable-update-logic
https://redux-toolkit.js.org/usage/usage-guide#simplifying-reducers-with-createreducer