Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React Redux reducer crud

import {
  CREATE_TUTORIAL,
  RETRIEVE_TUTORIALS,
  UPDATE_TUTORIAL,
  DELETE_TUTORIAL,
  DELETE_ALL_TUTORIALS,
} from "../actions/types";

const initialState = [];

function tutorialReducer(tutorials = initialState, action) {
  const { type, payload } = action;

  switch (type) {
    case CREATE_TUTORIAL:
      return [...tutorials, payload];

    case RETRIEVE_TUTORIALS:
      return payload;

    case UPDATE_TUTORIAL:
      return tutorials.map((tutorial) => {
        if (tutorial.id === payload.id) {
          return {
            ...tutorial,
            ...payload,
          };
        } else {
          return tutorial;
        }
      });

    case DELETE_TUTORIAL:
      return tutorials.filter(({ id }) => id !== payload.id);

    case DELETE_ALL_TUTORIALS:
      return [];

    default:
      return tutorials;
  }
};

export default tutorialReducer;
Comment

PREVIOUS NEXT
Code Example
Javascript :: Using a decrementing For Loop to Reverse an Array 
Javascript :: how to create an async function from a string in node js 
Javascript :: jQuery download video from URL 
Javascript :: can we use setstate inside build 
Javascript :: react clear input after button click 
Javascript :: check if all array elements are equal 
Javascript :: node import json 
Javascript :: javascript loading animation 
Javascript :: vscode jest disable auto run 
Javascript :: bash sort json alphabetically 
Javascript :: google scripts string split 
Javascript :: jquery public function 
Javascript :: sequelize findall 2 attributes 
Javascript :: how to extract strings in array js 
Javascript :: hover on child from parent mui react 
Javascript :: how to make html with jquery 
Javascript :: get date in milliseconds javascript 
Javascript :: javascript export multiple function 
Javascript :: slice() in js 
Javascript :: import downloadcsv from "vue-json-csv"; 
Javascript :: servicenow gliderecord lookup 
Javascript :: javascript compare timestamp 
Javascript :: useEffect() Execute Function When React Component Loads 
Javascript :: stale element reference: element is not attached to the page document 
Javascript :: how to create json file in android programmatically 
Javascript :: import slider material ui 
Javascript :: get search value from reacr route 
Javascript :: sequelize documentation 
Javascript :: palindrome number 
Javascript :: how to get 3rd li using jquery 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =