vendredi 3 août 2018

Difference between React Ref property by string and function

I'm new to React web development, and I'm studying about Component ref. I found this note:

You can allocate a function as a ref property of JSX. This function runs only once when the element is mounted. In this function, you can put a DOM node into instance's property.

<input ref={(input) => {this.emailInput = input}}
  className="form-control"
  type="text"
  placeholder="cad.co" />

In current official document, it is recommended to use function for ref, and it is mentioned that string ref will likely be deprecated in future version(https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs).

I roughly understand that refs and ref are used as reference variable, and also that for ref property string literal can be used.

But I don't get the point about how to use ref property with function argument. How can it be the same or similar to the work of using string property for ref?




Aucun commentaire:

Enregistrer un commentaire