类型定义

types/react 类型定义中对useRef函数定义了三次(两次重载

  1. /**
  2. * `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
  3. * (`initialValue`). The returned object will persist for the full lifetime of the component.
  4. *
  5. * Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
  6. * value around similar to how you’d use instance fields in classes.
  7. *
  8. * @version 16.8.0
  9. * @see https://reactjs.org/docs/hooks-reference.html#useref
  10. */
  11. function useRef<T>(initialValue: T): MutableRefObject<T>;
  12. // convenience overload for refs given as a ref prop as they typically start with a null value
  13. /**
  14. * `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
  15. * (`initialValue`). The returned object will persist for the full lifetime of the component.
  16. *
  17. * Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
  18. * value around similar to how you’d use instance fields in classes.
  19. *
  20. * Usage note: if you need the result of useRef to be directly mutable, include `| null` in the type
  21. * of the generic argument.
  22. *
  23. * @version 16.8.0
  24. * @see https://reactjs.org/docs/hooks-reference.html#useref
  25. */
  26. function useRef<T>(initialValue: T|null): RefObject<T>;
  27. // convenience overload for potentially undefined initialValue / call with 0 arguments
  28. // has a default to stop it from defaulting to {} instead
  29. /**
  30. * `useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
  31. * (`initialValue`). The returned object will persist for the full lifetime of the component.
  32. *
  33. * Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
  34. * value around similar to how you’d use instance fields in classes.
  35. *
  36. * @version 16.8.0
  37. * @see https://reactjs.org/docs/hooks-reference.html#useref
  38. */
  39. function useRef<T = undefined>(): MutableRefObject<T | undefined>;

image.png
第二个重载返回的RefObject 对象中current属性是只读属性
image.png

第一次定义

function useRef<T>(initialValue: T): MutableRefObject<T>;
如果函数调用匹配到这个定义,则 ref.current 类型只能是initialValue类型
初始值为boolean,没有传入T,则类型推断T为boolean,则始终只能是boolean类型
image.png

第二次定义(重载)

function useRef(initialValue: T|null): RefObject;

current值可以是T或者null 两种类型, 常见于获取dom元素
因为可能为null,所以确定存在,可以使用!非空断言
image.png
还有一种非空断言写法如此,Ref对象也改变了,从RefObject 变成MutableRefObject了
并且匹配到第一次定义(目前这个写法还没完全搞明白)
image.png
image.png

第三次定义(重载)

  1. function useRef<T = undefined>(): MutableRefObject<T | undefined>;

自定义hook—useRefObject

  1. import { useRef } from 'react'
  2. export function useRefObject<T>(t: T) {
  3. const ref = useRef<T>(t)
  4. ref.current = t
  5. return ref
  6. }

使用这个hook,除了初始化useRef,每次render都会执行一遍ref.current = t, 那它的应用场景就应该是current值依赖别的状态。
比如说,依赖到props值,当props值变化时,也要同步改变。正常要使用useEffect

  1. const ref = useRef(props.parmas1)
  2. useEffect(() => {
  3. ref.current = props.parmas1
  4. }, [props.params1])

但是使用useRefObject 就可以省略useEffect.