Reactガイドを読んでいくその236

これは

Reactのガイドを読んでいく記事です。

ガイドのリンク

ja.reactjs.org

フック早わかり

📌 ステートフック

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

上の例の中で useState がフックになる。
stateはReactによって再レンダーの間も保持される。
useStateは現在の値と、更新するための関数をペアにして返す。
useStateの引数はstateの初期値になり、上の例だと0になります。

今日はここまで