Is an alternative to the String attribute basically "legacy"?

I was a bit surprised when I read the updated version of React docs about links . Although their examples on the first page of React still use this.refs.textarea, they later "recommend" not to use this approach.

But what is the alternative? I find the embedded code too long, confusing and calling the function instead, for example {() => this.initChartLibrary() }( () =>it is necessary so as not to lose the context for this), it is necessary to create a method that gives a misconception that it can be reused.

In other words, I would still like to use String. But since I am afraid that they will depreciate this, I was wondering if there are any other approaches?

+4
source share
1 answer

As far as I know, the main alternative to strings refis the ES6 style callback function.

So:

<textarea ref="myTextArea">
...
</textarea>

// Where refs are then accessed by `this.refs.myTextArea` to perform any operations

Becomes in basic form:

<textarea ref={ (ref) => this.myTextArea = ref }>
...
</textarea>

// Where refs are then accessed by `this.myTextArea`

This can be quite powerful, because an attribute refin React automatically receives the referenced component as a parameter, which allows us to immediately execute our callback operations on it if it wants. This can be used in a simple form, as indicated above, to assign this link this.myTextAreaor, as you probably noticed in the docs, you can be a little more direct and do things like:

<input 
  ref = {(input) =>
          { if (input != null) {input.focus()} }
  } /> 

, , , . ( ), ref, , , .

, , React , - , (ref) => this.myRef = ref, refs this.myRef.

+3

All Articles