Answer the question
In order to leave comments, you need to log in
How can the High Order Reducer be improved?
Good afternoon! Can you please tell me how this High Order Reducer can be improved ?
const funcReducer = (type, initState, name, payload) =>
(state = initState, action = {}) => {
switch (action.type) {
case type[0]:
return {
[name]: {
...state[name],
data: payload ? action.payload[payload] : state[name].data,
},
};
case type[1]:
return {
[name]: {
...state[name],
isOpen: true,
data: !action.payload ? state[name].data : action.payload[payload],
},
};
case type[2]:
return {
[name]: {
...state[name],
isOpen: false,
error: '',
},
};
case type[3]:
return {
[name]: {
...state[name],
isLoading: true,
error: '',
},
};
default:
return state;
}
};
export default funcReducer ;
const types = [
'',
'OPEN_MODAL',
'CLOSE_MODAL',
'DELETE_REQUEST',
];
export const deleteItemReducer = funcReducer(types, initialState, 'delete', 'item');
data: !action.payload ? state[name].data : action.payload[payload]
is not needed everywhere. Answer the question
In order to leave comments, you need to log in
In my practice, such things only confuse the code. If you ask me, as they say, I would write it for several reducers myself. Easier to read, easier to maintain.
I believe that code should be written for people, not for a machine.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question