让函数组件具备 state 能力的 Hook

    1. import React, {useState} from 'react';
    2. const App = (props) => {
    3. const [count, setCount] = useState(0);
    4. return (
    5. <div>
    6. <div>{count}</div>
    7. </div>
    8. )
    9. }
    1. setCount(count + 1);
    2. setCount((count) => {
    3. count++;
    4. console.log(count);
    5. return count;
    6. });
    1. 直接赋值
    2. 使用回调函数 :::warning 使用 hook 的 setState 不会像类组件的 this.setState 会自动合并对象。所以要自己使用 … 剩余操作符作处理 :::