Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

counter react

import React from "react";

var CounterStateContext = React.createContext();
var CounterDispatchContext = React.createContext();

function counterReducer(state, action) {
  switch (action.type) {
    case "INC":
      return {...state, counter: state.counter + 1};
    case "DEC":
      return {...state, counter: state.counter - 1};
    case "MUL":
      return {...state, counter: state.counter * 2};
    case "plusNum":
      return {...state, counter: state.counter + parseInt(action.payload)};
    default: {
      throw new Error(`Unhandled action type: ${action.type}`);
    }
  }
}

function CounterProvider({children}) {
  var [state, dispatch] = React.useReducer(counterReducer, {
    counter: 5,
  });
  return (
    <CounterStateContext.Provider value={state}>
      <CounterDispatchContext.Provider value={dispatch}>
        {children}
      </CounterDispatchContext.Provider>
    </CounterStateContext.Provider>
  );
}

function useCounterState() {
  var context = React.useContext(CounterStateContext);
  if (context === undefined) {
    throw new Error("useLayoutState must be used within a LayoutProvider");
  }
  return context;
}

function useCounterDispatch() {
  var context = React.useContext(CounterDispatchContext);
  if (context === undefined) {
    throw new Error("useLayoutDispatch must be used within a LayoutProvider");
  }
  return context;
}

export {CounterProvider, useCounterState, useCounterDispatch, plusCounter, minusCounter, multiple2, plusNumber};

// ###########################################################
function plusCounter(dispatch) {
  dispatch({
    type: "INC",
  });
}

function minusCounter(dispatch) {
  dispatch({
    type: "DEC",
  });
}

function multiple2(dispatch) {
  dispatch({
    type: "MUL",
  });
}

function plusNumber(dispatch, number) {
  dispatch({
    type: "plusNum",
    payload: number
  });
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: queryselector multiple attributes 
Javascript :: react-drag-drop-files open twice 
Javascript :: lazy loading by scroll vue 
Javascript :: combineReducers. 
Javascript :: JS how to access a class propert 
Javascript :: electron js 
Javascript :: react onclick remove component 
Javascript :: get item in array from index 
Javascript :: fetch in vue 3 
Javascript :: javascript benchmark 
Javascript :: js sort array 
Javascript :: javascript map() method 
Javascript :: JavaScript - HTML DOM Methods 
Javascript :: connect redux 
Javascript :: for loop in javacript 
Javascript :: end of file expected json 
Javascript :: javascript ajax post send an object 
Javascript :: http error 406 
Javascript :: useeffect cleanup function 
Javascript :: what is bom in javascript 
Javascript :: aws lambda function setup for node js 
Javascript :: javascript templates 
Javascript :: redux toolkit reducer 
Javascript :: get last item in array js 
Javascript :: A closure Function 
Javascript :: vue component naming convention 
Javascript :: !! javascript 
Javascript :: js or operator 
Javascript :: react hooks useeffect 
Javascript :: where is brazil located 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =