I created a global error handling slice with redux toolkit. And I would like to refactor it to make it more "dry":
const errorsSlice = createSlice({
name: "error",
initialState,
reducers: {
clearError: (state) => {
state.errors = null;
state.isOpen = false;
},
},
extraReducers: {
[createScript.rejected]: (state, { payload }) => {
const errorArray = Object.values(payload.message).map(
(key) => key.message
);
state.errors = errorArray;
state.isOpen = true;
},
[createScene.rejected]: (state, { payload }) => {
const errorArray = Object.values(payload.message).map(
(key) => key.message
);
state.errors = errorArray;
state.isOpen = true;
},
},
});
The 2 extraReducers do the exact same thing and the payload is normalized. My code works fine as it is.
Is there a way to "combine" the 2 to a single extraReducer (at the end it will be much more)?