Instagram
youtube
Facebook
Twitter

Redux- Reducer

Reducer

  • Reducers are pure functions and are used to change states in Redux.
  • The reducer function accepts the previous state of app and action and then calculates the next state and returns a new object.
  • Syntax:
(state,action) => newState
  • Example:
const initialState = {
   isLoading: false,
   items: []
};
const reducer = (state = initialState, action) => {
   switch (action.type) {
      case 'ITEMS_REQUEST':
         return Object.assign({}, state, {
            isLoading: action.payload.isLoading
         })
      case ‘ITEMS_REQUEST_SUCCESS':
         return Object.assign({}, state, {
            items: state.items.concat(action.items),
            isLoading: action.isLoading
         })
      default:
         return state;
   }
}
export default reducer;