showing results for - "usereducer hook in react js"
Gabriela
30 Jul 2016
1const initialState = {count: 0};
2
3function reducer(state, action) {
4  switch (action.type) {
5    case 'increment':
6      return {count: state.count + 1};
7    case 'decrement':
8      return {count: state.count - 1};
9    default:
10      throw new Error();
11  }
12}
13
14function Counter() {
15  const [state, dispatch] = useReducer(reducer, initialState);
16  return (
17    <>
18      Count: {state.count}
19      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
20      <button onClick={() => dispatch({type: 'increment'})}>+</button>
21    </>
22  );
23}
Vincent
06 Jun 2020
1import React, { useState, useReducer } from "react";
2import ReactDOM from "react-dom";
3
4import "./styles.css";
5
6function reducer(state, action) {
7  switch (action.type) {
8    case "add":
9      return [...state, action.item];
10    case "remove":
11      return [
12        ...state.slice(0, action.index),
13        ...state.slice(action.index + 1)
14      ];
15    default:
16      throw new Error();
17  }
18}
19
20function FavoriteMovies() {
21  const [movies, dispatch] = useReducer(reducer, [{ name: "Heat" }]);
22  const [newMovie, setNewMovie] = useState("");
23
24  const handleAddClick = () => {
25    if (newMovie === "") {
26      return;
27    }
28    dispatch({ type: "add", item: { name: newMovie } });
29    setNewMovie("");
30  };
31
32  return (
33    <>
34      <div className="movies">
35        {movies.map((movie, index) => {
36          return (
37            <Movie
38              movie={movie}
39              onRemove={() => dispatch({ type: "remove", index })}
40            />
41          );
42        })}
43      </div>
44      <div className="add-movie">
45        <input
46          type="text"
47          value={newMovie}
48          onChange={event => setNewMovie(event.target.value)}
49        />
50        <button onClick={handleAddClick}>Add movie</button>
51      </div>
52    </>
53  );
54}
55
56function Movie({ movie, onRemove }) {
57  return (
58    <div className="movie">
59      <span>{movie.name}</span>
60      <button onClick={onRemove}>Remove</button>
61    </div>
62  );
63}
64
65function App() {
66  return (
67    <div className="App">
68      <h2>My favorite movies</h2>
69      <FavoriteMovies />
70    </div>
71  );
72}
73
74const rootElement = document.getElementById("root");
75ReactDOM.render(<App />, rootElement);
76
Virgil
01 Feb 2016
1function init(initialCount) {  return {count: initialCount};}
2function reducer(state, action) {
3  switch (action.type) {
4    case 'increment':
5      return {count: state.count + 1};
6    case 'decrement':
7      return {count: state.count - 1};
8    case 'reset':      return init(action.payload);    default:
9      throw new Error();
10  }
11}
12
13function Counter({initialCount}) {
14  const [state, dispatch] = useReducer(reducer, initialCount, init);  return (
15    <>
16      Count: {state.count}
17      <button
18        onClick={() => dispatch({type: 'reset', payload: initialCount})}>        Reset
19      </button>
20      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
21      <button onClick={() => dispatch({type: 'increment'})}>+</button>
22    </>
23  );
24}
Lorenzo
29 Mar 2017
1// useReducer is a React hook function that accepts a reducer function, and an initial state this hook function returns an array with 2 values
2const [state, dispatch] = useReducer(reducer, initialState);
3
4
5const initialState = {
6  darkMode: false,
7};
8
9
10function reducer(state, action) {
11  switch (action.type) {
12    case 'DARK_MODE_ON':
13      return { ...state, darkMode: true };
14    case 'DARK_MODE_OFF':
15      return { ...state, darkMode: false };
16      default:
17        return state;
18  }
19}
Nassim
16 Aug 2019
1const [state, dispatch] = useReducer(reducer, initialArg, init);
Livio
05 May 2020
1const [state, dispatch] = useReducer(reducer, initialState);
queries leading to this page
use example react usereducerusereducer api callreact native usereducer documentation 27usereduce hookreact userreducer usereducer with object example react usereducerusereducer react jsusereducer rerenders usestatecan i return a component in usereducer 3fhow to create a reducer in reactreact usereducer with init functionusereducers reactusereducer apireact hooks context reducerimport usereducer reactreact usereducer 28auth using usereducer in reactuse reducer in react hooksusereduce in react nativereact js reduce updater function usereduverusereducers in react jsusereducer function to load to initial stateuse contexxt hookreact calling reducercan i use a function in usereducerwhat is usereducer in react hoksreact component reducerusereducer custom hookhow to use usereducer with api reactdispatch in usereducerusereducer react usosusereducer statereact when to use usereducerusereducer dispatch inside useffectreact native usereducer examplereact redux reducers examplereact usereaducerreact usereducer stateusereducer in react tsreact usereducer and usememousereducer what does it returnhow often do you us usereducer hookcontext api reducer usestateusereducer react explanationreact redux reducerhow to use usereducer with reduxtesting usereducerusereducer promisereact reducersreact usereducer in expressjsuse reducer examplereact usestate previous statedispatch usestatewhen to use reducer reactuseref reactusereducer current nullwhat is the action in usereducerusereducer hook react jsreact reducer codewhat is usereducer reactusereducer react js nedirreact usereducer examplesreact type usereducerwhen might you use usereducer over usestate in a react componentusereducer react hooks examplehow does reducer function of usereducer understand state when you dont pass in a state parameter in the dispatchreact class to hooks use reducer how to pass reducer function to usereducer hookreact userefusereducer browser reactreact userdeucerreact redux usereducerreact usereducer when useuse redux usereducer in react nativeconst 5b 5d 3d usereducer 28 29usereducer for class componentreact hooks use reducerhow to use usereducer in reactbasic example usereducerrect usecontextusereducer react loginreact hook reducerwhat is great about usereducer hookreact component usereducer reduce reactjspayload in usereducer examplereact use api hookusereducer set valuereact reducereact js usereducerwhy we use reducer in reactusereducer refactoringusereducer 2b default valuleusereducer with reduxusereducer is it react8usereducer reactwhat is use reucer in reactreact usereducer tutorialwhat does reducer do in redux reactreact hook apicreatereducer and usereducer example react usememo on usereduecerreact reducer hook simple examplereact reudc hookis redux and usereducerreducer examplereact call reducer reducers in reactreact js usereducer examplereact ghooks reducerusereducer importreact hooks referenceredux what is reducerusereducer dispatch payloadexample of usereducerreact native useref examplewhere do you define usereducerreact hook dispatch callbackusereducer getusereduce reactusereducer react in class componentusereducer initial state as objectreact context reducer hooksusereducer idreact usereducer mdnreduce in react jsusereducer hooks rocketinit hook reactjsis hook and use state samewhat does the usereducer function do 3fusereducer react reduxhow to use usereducer and contexxt api in reactdo you need to spread in previous state with react usereducerreact usereducer class componentusereducer react ruterreact reducer jshow to change a value in usereducer in react jshow to use usereducer in react mediumusereducer objectreact reducer examplereducer en react jsreactjs when to use usereducerhow to create reducer in reduxusereducer returnusereducer argumentsreference hook reactreact hook reducer useeffect updat stateusereducer react praticeusereducer propertieswhat usereducer in react jsuse reducer expmapleclass component in react with usereducerusecallback on reducer functionusereducer explain with exampleusereducer init reactreact simpler usereducerusereducer with non ui component in react jsstate usereducer in reactwhen to usereducer reacthow to usereducer for an apireact functional usereducerstate value in usereducer reactjsusereducer trong reactjsreducer reactjsusereducer react exampleusereducer renderwhat is usereducer in react hookswhat is a reducer in reactuserreducer function refactoring usereducer react udereducer reactreact hooks apiusing usereducer in react nativeusereducer hook in react jsreact import usereducerusereducer in react mediumreact usereducer documentationwhy use react usereducerusereducer class componentreact what is usereducerusereducer in class componentreact useref combined with usereducerwhen need usereducer reactusereducer function deefinitionusereduce in reactconst 5bstate 2c dispatch 5d 3d usereducer 28reducer 2cwhat is a react reducerwhen you call usereducer and pass in a reducer and an initial state object 2c what does it return 3fusereducer 26 dispatch reactusereducer api detailusereducer callbackwhat is the reducer hookuse reducer in reactjsreact hoor usereducerhow to use reducer in reactreducer reactwhat is usereducer in react jsreact usereducer 28 29 documentationusereducer in react jsreact hooks usereducer examplereactjs usereduceruse reducer react from childunderstanding usereducer inistial stateusereducer jsreact usereducer examlehook usereducerusereducer code exampleusereducer react hooksuse reducer reactusereducer hook docusereducer in reduxwhat is the use of usereducer exampleusereducer in react native examplehow to call reducers function in reactuser educer in reactcan you use usereducer on class components reactdispatch react hookswhat is usereducer hook for and what does reducer do 3fhow often is usereducer used in reactreact usereducer mediumreact reducer hookssyntax of usereducerreact what are reducersuse reducer hookreact usereducer inside componentwhat does userreducer returnreact js coding for reduce methodexample when to use usereducerusereducer in classreact usereducer in class componentwhen to use usereducer reactusereducer 28 29 hook react usereducer react hook importinitail state usereducerreact js use reducerreact native context dispatch inputjavascript create own usereducerwhere to use usereducer in reactstate value in usereducer reactjs single valueuseeffect dispatch usereducerreact testing usereducerusereducer react arraysreact usereducerreact native usereducer documentationusereducer inside functionusereducer in react nativedispatch hook reactusereducer example with payloadreact reducer hookhow to make usereducer in react hookswhat is use of usereducer in reactuse redcuer examplereact dispatch hooksreducer hook in reactuse reducer with hooksexplanation of usereducer in reactjsexamples of usereducer in reactusereducer of reactreact usereducerreact onclick dispatch usereduce3rreducer hook reactcall usereducer in class componentreact why we use usereducerhow to use usereducer hooks in reactreact useeffect usereducerusereducer hook in react exampleusereducer hooks reactcreate a well designed reducer in reactusereducer update in normal functionause reduser in reactreact usereducerfusereducer implementation in reactreact js usereducerusereducer sign up react hookusereducer hook explainedwhat does usereducer hook dowhat is reducer in reactwhy usereducer hookuserreducer react functionusereducer in reactjsreact usereducer usereact how to use usereducer to handle statereducer context reactwhat are the advantage of using usereducer in react jsusereducer syntaxreact reducer with multiple state and useeffectwhat vestion of react have usereaducerreact usereducer objecthow to use usereducer hookusereducer re renders usestatereact usereducerusereducer react init funcreact hooks prev statereact userecucerreactjs when to usereducerusereducer payloadequivalent usereducer in class react componentusereducer hook examplereact reduce 28 29example usereducero que c3 a9 use reducer reactreact why use usereducerhow does the reducer function understand the state in usereducerreact native react usereducerusereducer hook usagereact usememo unlessusereducer react frreducer meaning reduxreact hook usereducerreact 2c simple usereducer exampleusereducer in react hooks examplereact hook 22usereduceruse action reducer react native hookwhen to usereduce reactreducer total in react jsusereducer update with new payloadusereducer in react domusereducer calculette react jsusereducer with out function reduce in reactwhat is the reducer in react js reduxreactjs usereduceusereducer documentationuesreducer in reactusereducer dispatch useffectcreate reducer and usage redux reacthow to call a usereducer in reactusereducer 28 29react usereducewrusereducer jsxusereducer reactreact context reducer dispatchhow to use usereducer in react contex apireact dispatch usestatereact reducer sampleusereducer reducer get state valueusereducer example reactwhat is reducer in react jsusereducer example in react jsusereducer react hooks how to have functions passed downreact native usereducer codereact hooks dispatchreact usereducer map reduceruseref example react jsusereducer initial state functionuseref hookreactjs usereducer hookreact usereducer syntaxusereducer hook in react documentationreducers react hooksuse of usereducer hookusereducer with resuce stateusereducer react nativereact usereducer explain and exampleusereducer in react providerusereducer state managementreact reducer functionreact useredcuer exampleswhat does the usereducer do in reactusereducer method in react hookshow to use reduce in react jsreact usecontext with usereduceractual use of usereducer in reactreact hooks reducer returning functionusereducer setwhat is reducer in reduxreact useresouce examplecreate reducer react reduxhow to use the usereducer statement in reactuse reducer in componentcan we use usereducer instead of reduxuse usereducer in reactreact 17 usereducerreact usereducer payloadusereducer 28 29 react react hooks provider as well as usecontextreact hooks usereducerreact usereducer exampleusereducer hook in reactreact useref hookreact context reducer custom usereducer hookuseeffect react dispatchreact how to use usereducerreact hook context reducerwhat is react reducerwhat does usereducer do in reactusereducer react routerreact userreducer datausereduce 28 29 reacgwhat is usereducer hook in reactusereducer parametersuserreducer and use refreactjs when to use reducerreact usereducewhat is the action of a usereducerreact reducers explainedwhen to use usereducer and reduxhow to use reducer in react hooksreact usereducer nedirwithreducer in reactjswhat are reducers reactwhat is dispatch hook in reactwhen we use usereducer in reactset reducer reactjsusereducer hook reactusereducer react exampleswhat are reducers in reactusereducer hook with reduxreduce react meyhod hooksreact hooks reducer exampleuse reducer example with useeffectsusereducer reactjs docsreact usereducer examplereactjs redux reducerreact usereducer custom hookuse reducer in react returnsusereducer wikiis usereducer used in reduxreducer function in reactreact typescript usereduceruse reducer as constant stateusereducer callback function to initial stateusereducer hook initialiaze functionreact usereducer hookusereducer reactjsuse reducer in reactusereducer hookreducer example in react jsdispatch in react hooksusereducer in reacthow to pass usereducer react hookswhat is a reducer in reduxusereducer for class component of reacthow to subscribe to changes when using react usereducerwhy use usereducer hook reactreact withreducerreact js redux reducerpass arguments to dispatch react js hooksusereducer with parameterswhat is usereducer equivalent in class componentexample of usereducer reactusereducer in contect apireact native usecontextusereducers in class componentusestate previous statewhat is a redux reducerif will use callback in action what is the role of reducer react nativeusereducer react hookreducer nedir reactusereducer 2b reactuseref in reactreact usereducer hook exampleusereducer react samplereduce in reactredux usereducerreducer in react hooksreduce reactwhat is a reducer reactwhat is the reducer in reduxredux reducer examplewhat does usereducer returnhow to set value to reducer reactwhat are react reducersusereducer hook initreact export usereducerusereduce 28 29 reactusereducer reduxwhy we using dispatch as second argument in useeffectusereducer javascriptreact hook useeffect dispatchusereducers in reactreact usereducreusereducer in jsusereducer classspreader in usereducer hookreact usereducer and reducerhow to react usereducer worksreact usereducer 28mytable reducerusereducer detail examplewhy usereducer is use in reactreact usereducer dispatchdifference between useeffect and usestateuserreducer reactuse of usereducer in reactacces usereducer state reactadvantages of usereducer in reacthow to set initial value usereducer hook in reactreducer in reduxreduce function in reactwhat is usereducer in reactreact usereducer explainedwhy use usereducer hookupdate reducer state in react usereducerreact hooks reducerwhat is usereducer hookusereducer in react examplereact reducer hook examplereact use reducerusereducer react moduleusereducer functionreact router dom with usereducerusing usereducerreduceer in reactusereducer actionusereducer hook in react nativeall react hooksexample of usereducer hookusecallback with usereducewhy we use usereducer in reactreducer react hooksunderstanding dispatch in usereducerreact hooks reducerswhat to use as the init ragument of usereducer in reactreact onclick dispatch usereducerusereducer payload examplereducer react examplereact reducers examplelist of hooks in reactuse imperative handleusereducer call apiwhat is react usereducerusereducer hook mediumis usereducers used often in reactusereducer and reduxwhat does react usereducer hook take as input and what does it return 3fusereducer hooks in reactwhat is difference between usestate directly change state or use callbackredux with react reducerreducer in reactusereducer in react hooksusereducer hook documentationusereducer example react hooksusereducer react js exampleusereducer with getstateuse reducerhow to practice using usereducer react jsusereducer 28 29 hookusing dispatch usereducerreactjs reducerreact native use reducer hookusing usereducer with reduxreducer redux exampleusereducer usesreact usereduerwhat is the usereducer hookstate object context reducer reacthow to get value in 5but in react js hoockusereducer use in reactreact reducer hook class componentusereducer react js implementwhat is the use of usereducer in reactreactjs usereducer exampledispatch all function react hooreact useredeucerreact reducerpurpose of react usereducerusereducer in functional component exampleusing usereducer in reactusedebug hookusereducerwhat is usereducer 28 29 in react 3fusereducer exampleusereducer react jsusereducer trong reactusing usereducer over usestateusereducer hook in react js