React Ref's Deprecated - What's the replacement?

Viewed 1927

I'm following an older textbook that is using refs throughout the components but refs no longer even work. It's been fully deprecated. The one question I found regarding this was asked in 2018 before refs were deprecated and all the responses are along the lines of "there is no evidence that refs are going to be deprecated so just keep using it."

Well, now it's been fully deprecated and no longer useable. I'm just wondering what the replacement is? Typically when they deprecate something there is an explicit replacement that they provide in the docs but I wasn't able to find anything useful for myself regarding refs. The documentation is all about ref callbacks and what not but my textbook isn't using ref callbacks. The author is just putting a ref attribute into an element and then destructuring the variable for use:

const { target } = this.refs

/* CODE */

<div ref="target"></div>
2 Answers

For class components React.createRef and funtion components React.useRef

const myRef = React.createRef()
<SomeComponent ref={myRef} />

// or

const myRef = React.useRef()
<someElement ref={myRef} />

This might help you:

https://medium.com/@rossbulat/how-to-use-react-refs-4541a7501663

Try also something like:

<MyComp callingMe = {this}  ... />

So you could use props.callingMe to use the methods/functions/vars/state related to this.

Related