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

PREVIOUS NEXT
Code Example
Javascript :: repeat call n times in js 
Javascript :: convert boolean to string javascript 
Javascript :: create functional component react 
Javascript :: modal react form table 
Javascript :: javascript unary plus and negation operators 
Javascript :: play audio file in phaser 
Javascript :: .change() in pure js 
Javascript :: index and id togtgher angularjs 
Javascript :: comparare due array di numeri javascript 
Javascript :: get the key of largest json array value 
Javascript :: node js download image from url as buffer 
Javascript :: why does javascript let you write a function without the parentheses 
Javascript :: set cookie in javascript 
Javascript :: ethers.js get time 
Javascript :: image name validate using regex javascript 
Javascript :: how to remove only green background from video using ffmeg nodejs 
Javascript :: substring in javascript 
Javascript :: toastr fades away disappears immediately quickly 
Javascript :: choco node 17 
Javascript :: confirm closing tab 
Javascript :: obtain only integer not decimal js 
Javascript :: javascript this Inside Constructor Function 
Javascript :: hot get access_token instead of url 
Javascript :: Setting up an Express project 
Javascript :: vue back image 
Javascript :: Nextjs mongodb connection setup 
Javascript :: custom css mui 
Javascript :: Match All Letters and Numbers freecodecamp 
Javascript :: custom hook 
Javascript :: javascript not running 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =