LoginSignup
0
0

More than 3 years have passed since last update.

React-Hooksはstateよりreducerの方がサンプルとしてわかりやすい気がした

Posted at

よくカウンターの例として state と setState を見る気がするけど、実は useReducer の方がわかりやすいのでは仮説。


import React from "react";
import ReactDOM from "react-dom";

function useCounter() {
  return React.useReducer((state, i) => state + i, 10);
}

function App() {
  const [count, countUp] = useCounter();
  return (
    <div className="App">
      <h1>count: {count}</h1>
      <h2>
        <button onClick={() => countUp(1)}>count up</button>
        <button onClick={() => countUp(-1)}>count down</button>
      </h2>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

けど、どうなんだろう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0