“użytkownik” Kod odpowiedzi

użytkownik

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;
fawad ahmed

użytkownik

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>
    </>
  );
}
Dangerous Dunlin

Reager UserEducer

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;
Mateo Jaramillo

użytkownik

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>
    </>
  );
}
Dangerous Dunlin

użytkownik

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;
Blue Baboon

Odpowiedzi podobne do “użytkownik”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu