xxxxxxxxxx
const reducer = (state = initialState, action) => {
switch (action.type) {
case "BUY_CAKE":
return {
numOfCakes: state.numOfCakes - 1,
};
default:
return state;
}
};
xxxxxxxxxx
const Reducer = (state=[],action) =>{
switch(action.type){
case'add':
return [state,action.payload]
default:
return state ;
}
}
xxxxxxxxxx
// (state, action) => newState
function todosReducer(todos = [], action = {}) {
switch (action.type) {
case 'ADD_TODO':
return [todos, action.payload];
case 'DELETE_TODO':
return todos.filter((todo) => todo.id !== action.payload.id);
case 'DELETE_ALL_TODO':
return [];
case 'TOGGLE_TODO':
return todos.map((todo) => {
if (todo.id === action.payload.id) {
return { todo, complete: !todo.complete };
}
return todo;
});
default:
return todos;
}
}
xxxxxxxxxx
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
}
xxxxxxxxxx
{/**The logic inside reducer functions typically follows the same series of steps:
Check to see if the reducer cares about this action
If so, make a copy of the state, update the copy with new values, and return it
Otherwise, return the existing state unchanged
Here's a small example of a reducer, showing the steps that each reducer should follow:
*/}
const initialState = { value: 0 }
function counterReducer(state = initialState, action) {
// Check to see if the reducer cares about this action
if (action.type === 'counter/incremented') {
// 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
}
//Reducers can use any kind of logic inside to decide what the new state should be: if/else, switch, loops, and so on.
xxxxxxxxxx
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;
}
};