昨日の自分よ、見てるか~‘?
参考: 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 とかの新しい書き方ってこと?