Skip to content

noReactStringRefs (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"suspicious": {
"noReactStringRefs": "error"
}
}
}
}

Disallow string refs in React components.

String refs are a legacy React feature. Modern React code should use callback refs, createRef(), or useRef() instead.

Biome also flags template literal refs, even though upstream only does so through an option.

function Hello() {
return <div ref="hello">Hello</div>;
}
code-block.jsx:2:19 lint/suspicious/noReactStringRefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ String refs are deprecated.

1 │ function Hello() {
> 2 │ return <div ref="hello">Hello</div>;
│ ^^^^^^^
3 │ }
4 │

ℹ String refs are a legacy React feature that can make ref usage harder to follow.

ℹ Use a callback ref or an object ref created with createRef() or useRef() instead.

function Hello({ id }) {
return <div ref={`hello-${id}`}>Hello</div>;
}
code-block.jsx:2:19 lint/suspicious/noReactStringRefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ String refs are deprecated.

1 │ function Hello({ id }) {
> 2 │ return <div ref={`hello-${id}`}>Hello</div>;
│ ^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ String refs are a legacy React feature that can make ref usage harder to follow.

ℹ Use a callback ref or an object ref created with createRef() or useRef() instead.

class Hello extends React.Component {
componentDidMount() {
this.refs.hello.focus();
}
}
code-block.jsx:3:5 lint/suspicious/noReactStringRefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Using this.refs is deprecated.

1 │ class Hello extends React.Component {
2 │ componentDidMount() {
> 3 │ this.refs.hello.focus();
│ ^^^^^^^^^
4 │ }
5 │ }

ℹ Accessing refs through this.refs relies on React's legacy string ref behavior.

ℹ Store the ref on an instance field with a callback ref, or switch to createRef() or useRef().

function Hello() {
const helloRef = useRef(null);
return <div ref={helloRef}>Hello</div>;
}