Existen tres formas de crear un reducer:
const reducer = (state, action)=>{
if(action.type==="ERROR"){
return {
...state,
error: true,
loading: false
}
}else if(action.type==="CHECK"){
return{
...state,
loading: true,
}
}else{
return {...state};
}
const reducer = (state, action)=>{
switch (action.type){
case "ERROR":
return {
...state,
error: true,
loading: false
}
case "CHECK":
return{
...state,
loading: true,
}
default:
return {
...state,
}
}
}
const reducerOBJECT = (state) => ({
'ERROR': {
...state,
error: true,
loading: false,
},
'CONFIRM': {
...state,
error: false,
loading: false,
confirmed: true,
},
})
const reducer = (state, action) => {
if (reducerOBJECT(state)[action.type]) {
return reducerOBJECT(state)[action.type];
} else {
return {
...state,
}
}
}