React Hooks

简单说,Hooks 是一些函数,可以让函数组件也拥有原本只有类组件才能用的功能。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import { useState, useEffect } from 'react';

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

  useEffect(() => {
    document.title = `点击了 ${count} 次`;
  }, [count]); // 只有 count 改变时才执行

  return (
    <div>
      <p>你点击了 {count} </p>
      <button onClick={() => setCount(count + 1)}>点我</button>
    </div>
  );
}

所有的hooks都是用use开头的,常见的比如

useState,useEffect,useContext

useRef.

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy