Why this matters
Using string refs in React is deprecated and makes the component slower, harder to maintain, and less composable. Use function-based refs instead.
Ensure that string refs are not used in React. String refs are deprecated, make components harder to maintain, and reduce composability. Recommend using function-based refs instead.
Using string refs in React is deprecated and makes the component slower, harder to maintain, and less composable. Use function-based refs instead.
Side-by-side examples engineers can pattern-match during review.
const Hello = createReactClass({
componentDidMount() {
const component = this.refs.hello; // Noncompliant
// ...
},
render() {
return <div ref="hello">Hello, world.</div>;
}
});const Hello = createReactClass({
componentDidMount() {
const component = this.hello;
// ...
},
render() {
return <div ref={(c) => { this.hello = c; }}>Hello, world.</div>;
}
});const Hello = createReactClass({
componentDidMount() {
const component = this.refs.hello; // Noncompliant
// ...
},
render() {
return <div ref="hello">Hello, world.</div>;
}
});const Hello = createReactClass({
componentDidMount() {
const component = this.hello;
// ...
},
render() {
return <div ref={(c) => { this.hello = c; }}>Hello, world.</div>;
}
});From the same buckets as this rule.