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 :: how to check if an element already exists in an array in javascript 
Javascript :: sails js 
Javascript :: javascript code for find the last element in array 
Javascript :: nuxt 3 in beta release 
Javascript :: pdf to image javascript 
Javascript :: rest parameter 
Javascript :: react window navigate 
Javascript :: new line in textarea javascript 
Javascript :: classlist toggle 
Javascript :: code splitting react 
Javascript :: what is palindrome 
Javascript :: Routes & GET Requests 
Javascript :: javascript null 
Javascript :: javascript split array 
Javascript :: fetch timeout 
Javascript :: Unexpected token < in JSON at position 0 
Javascript :: convert number into string 
Javascript :: Texto unitário no node js 
Javascript :: mapview hooks glitch 
Javascript :: AND Or condition in text with bracket how to divide in javascript 
Javascript :: get table schema with knex 
Javascript :: react native gridient button 
Javascript :: neo4j get first 3 nodes 
Javascript :: npm ln 
Javascript :: “new Set” is returning an empty set in nodejs 
Javascript :: proet javascript web 
Javascript :: chandrachaan 
Javascript :: bad site theme 
Javascript :: How to use wildcard in Jason_VALUE 
Javascript :: react enzyme mount ReferenceError: is not defined 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =