mini_React icon indicating copy to clipboard operation
mini_React copied to clipboard

useReducer有问题啊,没法更新

Open yzw2017 opened this issue 8 months ago • 0 comments

/*

  • @Author: changcheng
  • @LastEditTime: 2023-08-24 11:50:03 */ import * as React from "react"; import { createRoot } from "react-dom/client";

function reducer(state, action) { if (action.type === 'incremented_age') { return { age: state.age + 1 }; } return state }

function App() { const [state, dispatch] = React.useReducer(reducer, { age: 42 });

console.log(state)

function handleClick() { dispatch({ type: 'incremented_age' }) }

return ( <button onClick={handleClick}>{state.age} ); } const root = createRoot(document.getElementById("root")); root.render(<App />);

yzw2017 avatar May 30 '24 02:05 yzw2017