What's the difference between `useRef` and `createRef`?

后端 未结 5 775
感动是毒
感动是毒 2020-11-29 22:57

I was going through the hooks documentation when I stumbled upon useRef.

Looking at their example…

function TextInputWithFocusButton() {
  const inpu         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 23:06

    createRef always returns a new ref, which you'd generally store as a field on a class component's instance. useRef returns the same ref upon every render of a functional component's instance. This is what allows the state of the ref to persist between renders, despite you not explictly storing it anywhere.

    In your second example, the ref would be re-created upon every render.

提交回复
热议问题