showing results for - "usereducer"
Max
27 Mar 2019
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}
Lorenzo
27 Oct 2016
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
Nele
29 Nov 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}
Albany
03 Jan 2021
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}
Ilaria
02 Jun 2016
1  const [state, dispatch] = useReducer(
2    reducer,
3    {count: initialCount}  );
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 reduceruse reducer in react hooksusereduce in react nativeupdater function usereduverusereducers in react jsusereducer function to load to initial stateuse contexxt hookreact calling reducercan i use a function in usereducerreact 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 exampleusereducer 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 usereducerreact reducer codewhat is usereducer reactusereducer react js nedirreact usereducer exampleswhen might you use usereducer over usestate in a react componenthow 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 redux usereduceruse redux usereducer in react nativeconst 5b 5d 3d usereducer 28 29react hooks use reducerhow to use usereducer in reactbasic example usereducerrect usecontextcreate context react hooksreact hook reducerwhat is great about usereducer hookreact component usereducer payload in usereducer examplereact use api hookusereducer set valuereact reducereact js usereducerwhy we use reducer in reactusereducer refactoringusereducer 2b default valuleusereducer with reduxusereducer is it 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 referenceusereducer 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 3fdo you need to spread in previous state with react usereducerreact usereducer class componentreact reducer 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 propertiesuse reducer expmapleusecallback on reducer functionusereducer explain with exampleusereducer init reactreact simpler usereducerusereducer with non ui component in react jsstate usereducer in reacthow to usereducer for an apireact functional usereducerstate value in usereducer reactjsreducer reactjsusereducer react exampleusereducer renderwhat is usereducer in react hookswhat is a reducer in reactuserreducer function refactoring usereducer react react context hooks exampleudereducer reactreact hooks apiusing usereducer in react nativeusereducer hook in react jsreact import usereducerusereducer in react mediumreact usereducer documentationreact hook update argumentreact what is usereducerusereducer in class componentreact useref combined with usereducerusereducer 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 callbackuse reducer in reactjshow to use reducer in reactwhat is usereducer in react jsreducer reactreact usereducer 28 29 documentationusereducer in react jsreact hooks usereducer examplereactjs usereduceruse reducer react from childunderstanding usereducer inistial stateusereducer jshook usereducerusereducer react hooksuse reducer reactusereducer hook docusereducer in reduxusereducer in react native examplehow to call reducers function in reactuser educer in reactdispatch react hookswhat is usereducer hook for and what does reducer do 3fhow often is usereducer used in reactreact usereducer mediumreact reducer hookssyntax of usereduceruse reducer hookwhat 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 usereducerstate value in usereducer reactjs single valueuseeffect dispatch usereducerwhen to use usereducer in react componentsreact testing usereducerreact native usereducer documentationusereducer inside functionusereducer in react nativedispatch hook reactreact reducer hookhow to make usereducer in react hooksusing context with react hooksuse 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 componentusecontext reacthow to use usereducer hooks in reactreact useeffect usereducercreate 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 reactusereducer syntaxreact reducer with multiple state and useeffectwhat vestion of react have usereaducerhow to use usereducer hookusereducer re renders usestatereact usereducerreact hooks prev statereact userecucerusereducer payloadequivalent usereducer in class react componentusereducer hook examplereact reduce 28 29example usereducero que c3 a9 use reducer reactreact why use 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 hookusereducer update with new payloadreducer total in react jsusereducer in react domusereducer with out functionusecontext in react reduce in reactwhat is the reducer in react js reduxreactjs usereduceusereducer documentationuesreducer in reactusereducer dispatch useffectcreate reducer and usage redux reactusereducer 28 29usereducer 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 hookreducers react hooksusereducer 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 hooks reducer returning functionusereducer setwhat is reducer in reduxreact useresouce examplecreate reducer react reduxuse reducer in componentcan we use usereducer instead of reduxreact 17 usereducerreact usereducer payloadusereducer 28 29 react react hooks provider as well as usecontextreact hooks usereducerusereducer 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 reacgusereducer parametersuserreducer and use refreactjs when to use reducerwhen to use usereducer and reduxhow to use reducer in react hooksreact usereducer nedirwithreducer in reactjswhat are reducers reactusecontext in react nativewhat 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 reactuse reducer as constant stateusereducer callback function to initial statereact 2c 28 usecontext 29usereducer hook initialiaze functionreact usereducer hookusereducer reactjsusereducer hookreducer example in react jsdispatch in react hooksusereducer in reacthow to pass usereducer react hookswhat is a reducer in reduxhow 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 componentusereducer in contect apireact native usecontextusereducers in class componentusestate previous stateif 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 sampleredux 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 dispatchreact usereducreusereducer classspreader in usereducer hookreact usereducer 28mytable reducerusereducer detail examplereact usereducer dispatchdifference between useeffect and usestateuserreducer reactuse of usereducer in reactacces usereducer state reacthow to set initial value usereducer hook in reactreducer in reduxreduce function in reactwhat is usereducer in reactreact usereducer explainedupdate reducer state in react usereducerreact hooks reducerwhat is usereducer hookusereducer in react examplereact reducer hook examplereact use reducerusestate with preusereducer react moduleusereducer functionreact usecontextreact 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 hooksreact hooks reducersreact onclick dispatch usereducerusereducer payload examplelist of hooks in reactuse imperative handlewhat is react usereducerusereducer hook mediumusereducer and reduxusereducer hooks in reactwhat is difference between usestate directly change state or use callbackreducer in reactusereducer in react hooksusereducer hook documentationusereducer example react hooksusereducer react js exampleusereducer with getstateuse reducerusereducer 28 29 hookusing dispatch usereducerreact native use reducer hookusing usereducer with reduxreducer redux exampleusereducer usesreact usereduerwhat is the usereducer hookstate object context reducer reactreact update hook argument how to get value in 5but in react js hoockusereducer use in reactreact reducer hook class componentusereducer react js implementreactjs usereducer exampledispatch all function react hooreact reducerpurpose of react usereducerusereducer in functional component exampleusing usereducer in reactusedebug hookusereducerusereducer exampleusereducer trong reactusing usereducer over usestateusereducer