LoginSignup
0
0

More than 3 years have passed since last update.

React Hookが何かをかいつまんで昨日の自分に教える

Posted at

昨日の自分よ、見てるか~‘?

参考: https://ja.reactjs.org/docs/hooks-intro.html

関数コンポーネントにステートを導入できる機能みたいだ。導入された理由の一つにビジネスロジックの分離?があるみたいだが理屈がよくわからないのでいったん忘れてよい。

sample code

import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

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

const [count, setCount] = useState(0);

countはステート変数
setCountはステートを変更する関数のようだ

useEffect はステート変更時のイベントをかけるみたい。

おわり

setState とかの新しい書き方ってこと?

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