Create a hook to tell if it is the first render.

useRef 所返回的对象在组件的生命周期中持续存在,能够用来判断是否为第一次渲染,useRef 会在每次渲染时返回同一个 ref 对象

  1. import {useRef} from 'react'
  2. export function useIsFirstRender() {
  3. // your code here
  4. const ref = useRef(0)
  5. ref.current++;
  6. return ref.current === 1;
  7. }