Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usereducer react

import React, { useReducer } from 'react'
import { render } from 'react-dom'

const types = {
  PET: 'PET',
  COLOR: 'COLOR',
}

const reducer = (state, action) => {
  switch (action.type) {
    case types.PET:
      return { ...state, pet: action.value }
    case types.COLOR:
      return { ...state, color: action.value }
  }
}

const initialState = {
  color: 'black',
  pet: 'cat',
}

export default function App() {
  const [state, dispatch] = useReducer(reducer, initialState)

  return (
    <div>
      <label>Choose a color and a pet: </label>
      <br />
      <select
        value={state.color}
        onChange={event => {
          dispatch({ type: types.COLOR, value: event.target.value })
        }}
      >
        <option value="black">Black</option>
        <option value="pink">Pink</option>
        <option value="blue">Blue</option>
      </select>
      <select
        value={state.pet}
        onChange={event => {
          dispatch({ type: types.PET, value: event.target.value })
        }}
      >
        <option value="cat">Cat</option>
        <option value="dog">Dog</option>
        <option value="mouse">Mouse</option>
      </select>
      <br />
      <br />
      You chose a {state.color} {state.pet}
    </div>
  )
}

render(<App />, document.querySelector('#app'))
Comment

useReducer

const initialState = {count: 0};

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    default:
      throw new Error();
  }
}

function Counter() {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
      <button onClick={() => dispatch({type: 'increment'})}>+</button>
    </>
  );
}
Comment

useReducer

import './App.css';
import { useState, useReducer } from 'react';


function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return ({ ...state, count: state.count + 1 })
    case 'decrement':
      return ({ ...state, count: state.count - 1 })
    case 'tgColor':
      return ({ ...state, color: (!state.color) })
    case 'handleInputChange':
      return ({ ...state, name: action.payload })
  }
}

function App() {

  const [state, dispatch] = useReducer(reducer, { count: 0, color: false, name: "" })

  // const [name, setName] = useState("")
  // const [count, setCount] = useState(0)
  // const [color, setColor] = useState(false);

  const handleInputChange = (e) => {
    dispatch({ type: "handleInputChange", payload: e.target.value })
  }

  const colorCode = "#b81f00";

  return (
    <div className="App">
      <form action="">
        <input type="text" onChange={handleInputChange} value={state.name} placeholder="Enter Name" />
      </form>
      <h3 style={state.color ? { color: "red" } : { color: "black" }}>{state.count}</h3>
      <button className="btn" onClick={() => dispatch({ type: "increment" })}>+</button>
      <button className="btn" onClick={() => dispatch({ type: "decrement" })}>-</button>
      <button className="btn" onClick={() => dispatch({ type: "tgColor" })}>Color</button>
      <h2 style={state.color ? { color: "red" } : { color: "black" }}>{state.name}</h2>
    </div>
  );
}

export default App;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

usereducer

import React from "react";
import { useReducer } from "react";
const initialState = {
  count: 0,
};
type ReducerProps = {
  state: any;
};
const reducer = (state: state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + action.payload };
    case "decrement":
      return { count: state.count - action.payload };
    default:
      return state;
  }
};

const Counter = () => {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    <div>
      Count : {state.count}
      <button
        onClick={() => {
          dispatch({ type: "increment", payload: 1 });
        }}
      >
        Increment ++
      </button>
      <button
        onClick={() => {
          dispatch({ type: "decrement", payload: 1 });
        }}
      >
        Decrement --
      </button>
    </div>
  );
};

export default Counter;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer React

import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
Comment

useReducer

function init(initialCount) {  return {count: initialCount};}
function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    case 'reset':      return init(action.payload);    default:
      throw new Error();
  }
}

function Counter({initialCount}) {
  const [state, dispatch] = useReducer(reducer, initialCount, init);  return (
    <>
      Count: {state.count}
      <button
        onClick={() => dispatch({type: 'reset', payload: initialCount})}>        Reset
      </button>
      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
      <button onClick={() => dispatch({type: 'increment'})}>+</button>
    </>
  );
}
Comment

useReducer() hook react

code snippet 

https://codesandbox.io/s/usereducer-demo-zxguu?file=/src/App.js
Comment

usereducer

  const [state, dispatch] = useReducer(
    reducer,
    {count: initialCount}  );
Comment

usereducer react

const [state, dispatch] = useReducer(reducer, initialArg, init);
Comment

what does the useReducer do in react

const [state, dispatch] = useReducer(reducer, initialState);
Comment

useReducer in react

const [state, dispatch] = useReducer(
    reducer,
    {count: initialCount}
  );
Comment

useReducer in react

function init(initialCount) {
  return {count: initialCount};
}

function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    case 'reset':
      return init(action.payload);
    default:
      throw new Error();
  }
}

function Counter({initialCount}) {
  const [state, dispatch] = useReducer(reducer, initialCount, init);
  return (
    <>
      Count: {state.count}
      <button
        onClick={() => dispatch({type: 'reset', payload: initialCount})}>
        Reset
      </button>
      <button onClick={() => dispatch({type: 'decrement'})}>-</button>
      <button onClick={() => dispatch({type: 'increment'})}>+</button>
    </>
  );
}
Comment

useReducer

function reducer(state, action) {
  switch (action.type) {
    case 'incremented_age': {
      // ✅ Instead, return a new object
      return {
        ...state,
        age: state.age + 1
      };
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: rgba to hex 
Javascript :: timer js 
Javascript :: web application development software 
Javascript :: what does the useReducer do in react 
Javascript :: ternary operator shorthand javascript 
Javascript :: javascript Prevent Object MutationPassed 
Javascript :: react datetime mannual editing 
Javascript :: how to upload document cloddinary 
Javascript :: django send and receive image data to react 
Javascript :: mapsort 
Javascript :: move li to bottom of list jquery selected value 
Javascript :: db.each store rowa 
Javascript :: functions like once 
Javascript :: load data table app script 
Javascript :: how to embed element in to array 
Javascript :: mongoose wont update value in array 
Javascript :: reract native stack yarn 
Javascript :: how to push into an array javascript 
Javascript :: front end display data frmo database nodejs html 
Javascript :: what is an ember pacjquery.slim.min.map 
Javascript :: como saber cuando un link cambia angular 
Javascript :: node-google-spreadsheet color border 
Javascript :: what is es11 
Javascript :: conflict paypal api javascript with user agent Mozilla/5.0 Google 
Javascript :: let scores = [80, 90, 70]; for (const score of scores) { console.log(score); } 
Javascript :: remove a function added to eventhandler 
Javascript :: get current page rows in tabulator js 
Javascript :: FORM EN JAVA SCRIPT 
Javascript :: how to exclude required files from grunt merge 
Javascript :: select xml child element with jQuery 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =