Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
React.js Ref Usage

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda