redux reducer
const reducer = (state = initialState, action) => {
switch (action.type) {
case "BUY_CAKE":
return {
numOfCakes: state.numOfCakes - 1,
};
default:
return state;
}
};
redux reducer
const reducer = (state = initialState, action) => {
switch (action.type) {
case "BUY_CAKE":
return {
numOfCakes: state.numOfCakes - 1,
};
default:
return state;
}
};
How reducer works in redux
const initialState = { value: 0 }
function counterReducer(state = initialState, action) {
// Check to see if the reducer cares about this action
if (action.type === 'counter/increment') {
// If so, make a copy of `state`
return {
...state,
// and update the copy with the new value
value: state.value + 1
}
}
// otherwise return the existing state unchanged
return state
}
redux reducer example
import * as ActionTypes from './ActionTypes';
export const comments = (state = { errMess: null, comments: []}, action) => {
switch (action.type) {
case ActionTypes.ADD_COMMENTS:
return {...state, errMess: null, comments: action.payload};
case ActionTypes.COMMENTS_FAILED:
return {...state, errMess: action.payload};
default:
return state;
}
};
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us