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

后端 未结 5 770
感动是毒
感动是毒 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:27

    Yet another but important addition to other's answers.

    You can't set a new value for createRef. But you can for useRef.

    const ur = useRef();
    const cr = createRef();
    
    ur.current = 10; // you can do it, and value is set
    cr.current = 10; // you can, but it's no good, it will not change it
    

提交回复
热议问题