String Refs

Learn how to programmatically bring focus to an element or get access to the actual DOM element using refs in string format.

What are String Refs?

The simplest and oldest form of refs are string refs. Nowadays, it is not recommended to use them because they can impact performance and might be deprecated in the future. We mentioned them for the sake of completeness because they are currently still part of the official API and you might come across them during your work with React (especially if you are working with legacy code).

How to use String Refs?

To define a string ref, you pass a prop named ref to a DOM element and pass a value to that prop in string form. The corresponding DOM element is now accessible to us using the instance method this.ref inside the component. Let’s look at an example to illustrate this.

Get hands-on with 1200+ tech skills courses.