3

I'm trying to understand usage of the Ref in React. I saw an example in the Ant Design documentation. https://3x.ant.design/components/tag/#components-tag-demo-control

There is a one line code that I couldn't get how it works.

  saveInputRef = input => (this.input = input);

And usage as follows:

<Input ref={this.saveInputRef} ...

But in the React documentation, it is said that you create a ref using React.createRef() method.

https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element

Is it an alternative way of using it? Why there is no React.createRef() method?

2
  • 2
    Whenever you want any type of changes directly in the real DOM, at that time Ref comes in the picture. For example, We have one video player component, but when we play/pause the video it will re-render the complete video player component and the video is starting of the beginning. so, to solve this type of issue, you can use Ref and directly change the real DOM. so after pause video will always where you left. Commented Sep 30, 2021 at 12:19
  • 4
    That's a callback ref Commented Sep 30, 2021 at 12:24

1 Answer 1

1

As far as I know there are 2 different kind of Refs, but they are commonly used together.

A. There is "Ref" created by "createRef" (or "useRef" when using hooks); This stores a value in the "current" property of the Ref. This value won't cause rerenders and is kept after rerenders.

B. And there is ref as a property of build-in components. This property is used to access the domnode.

Usually ref (B) is stored in a Ref (A)

You can, however, store whatever you'd like in Ref (A). And you don't need necessarily need to store a node gotten from ref (B) in a Ref (A), you can access it directly as well, that what this piece of code does:

<div ref={node => doSomething(node)}/>

Or simply

<div ref={doSomething}/>

This is called a "callback Ref":

Callback Refs

React also supports another way to set refs called “callback refs”, which gives more fine-grain control over when refs are set and unset.

Instead of passing a ref attribute created by createRef(), you pass a function. The function receives the React component instance or HTML DOM element as its argument, which can be stored and accessed elsewhere.

https://reactjs.org/docs/refs-and-the-dom.html#callback-refs

EDIT:

more about Ref (A) when using hooks.

Essentially, useRef is like a “box” that can hold a mutable value in its .current property.

You might be familiar with refs primarily as a way to access the DOM. If you pass a ref object to React with , React will set its .current property to the corresponding DOM node whenever that node changes.

However, useRef() is useful for more than the ref attribute. It’s handy for keeping any mutable value around similar to how you’d use instance fields in classes.

This works because useRef() creates a plain JavaScript object. The only difference between useRef() and creating a {current: ...} object yourself is that useRef will give you the same ref object on every render.

Keep in mind that useRef doesn’t notify you when its content changes. Mutating the .current property doesn’t cause a re-render. If you want to run some code when React attaches or detaches a ref to a DOM node, you may want to use a callback ref instead.

https://reactjs.org/docs/hooks-reference.html#useref

Sign up to request clarification or add additional context in comments.

3 Comments

Can’t a ref can be any value? Not just an object with a current property (Although that’s the main use).
something stored in a ref is always in the current propery I believe
@evolutionxbox yes. There is very few use cases where you need a React Ref. Refs are often used the wrong way so when you need one, make sure it's appropriate ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.