Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

redux reducer

const Reducer = (state=[],action) =>{
	switch(action.type){
      case'add':
        return [...state,action.payload]
      default:
        return state ;
    }
}
Comment

reducer function redux

{/**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.
Comment

redux reducer

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "BUY_CAKE":
      return {
        numOfCakes: state.numOfCakes - 1,
      };
    default:
      return state;
  }
};
Comment

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;
    }
};
Comment

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
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: this in javascript 
Javascript :: javascript get params from query string json object 
Javascript :: try catch with for loop in javascript 
Javascript :: Using Then To Create A Promise In JavaScript 
Javascript :: heroku buildpacks with react 
Javascript :: nested navigation react native 
Javascript :: react icon import 
Javascript :: glide.js autoplay 
Javascript :: reduce to calculate sum react 
Javascript :: why array.map returns undefined 
Javascript :: vuex do not mutate vuex store state outside mutation handlers. nuxt 
Javascript :: onclick hold react 
Javascript :: arrow functions in js 
Javascript :: javascript count up timer 
Javascript :: Detect the city on application launch via geolocation react native 
Javascript :: how to copy an arry react 
Javascript :: toisodatestring 
Javascript :: nestjs init 
Javascript :: using fb login with angular app 
Javascript :: classlist.contain in javascript 
Javascript :: How to loop through an object in JavaScript with the Object.values() method 
Javascript :: AngularJS how to use btn-group or radio group in list 
Javascript :: how to mock a library in jest 
Javascript :: split and convert a string into object 
Javascript :: capitalize each word from string in react 
Javascript :: js format indian price with commas 
Javascript :: js html editor 
Javascript :: javascript for...of with Strings 
Javascript :: firebase functions add to database 
Javascript :: react recoil 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =