noReactStringRefs (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v2.4.14 - Diagnostic Category:
lint/suspicious/noReactStringRefs - This rule is recommended, meaning it is enabled by default.
- This rule doesnβt have a fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
- Same as
react/no-string-refs
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "suspicious": { "noReactStringRefs": "error" } } }}Description
Section titled βDescriptionβ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.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβ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>;}Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.