no-string-refs
Configuration
rslint.config.ts
Disallow using deprecated string refs.
Rule Details
React used to support string refs (e.g. ref="name", then accessed via this.refs.name), but string refs are deprecated — they tie the ref to the component that rendered it (making composition surprising), interact poorly with <StrictMode>, and cannot be cleaned up automatically. Callback refs (ref={node => ...}) and React.createRef() / useRef() should be used instead.
This rule reports two things:
- A string literal (or, optionally, a template literal) used as a
refprop value:ref="hello",ref={'hello'},ref={\hello`}`. - Access of
this.refsinside an ES5createReactClass({...})component or an ES6 class extendingReact.Component/React.PureComponent. React 18.3.0 madethis.refswritable, so the check is skipped whensettings.react.versionis set to 18.3.0 or later.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Options
noTemplateLiterals
When true, template literals used as a ref value are also reported (by default only plain string literals are flagged).
Settings
settings.react.version— when set to a version>= 18.3.0,this.refsaccesses are not reported (they are writable on modern React). When unset, defaults to latest.settings.react.pragma— used to recognize<pragma>.createClass(...)and classes extending<pragma>.Component/<pragma>.PureComponent. Defaults toReact.settings.react.createClass— the identifier used for ES5 component factories. Defaults tocreateReactClass.
Differences from ESLint
- JSDoc
@extends/@augmentstags are not honored. eslint-plugin-react has anisExplicitComponentpath that treats a class as a React component when its JSDoc contains@extends React.Componentor@augments React.Component, even without anextendsclause. rslint only recognizes components through the actualextendsclause; JSDoc-only component declarations are not flagged. settings.react.version = "detect"is not resolved. eslint-plugin-react readsreactfromnode_modulesto auto-detect the version. rslint treats"detect"(and any non-numeric version string) as "latest" (999.999.999), which meansthis.refsis not reported in that mode. Set an explicit version string (e.g."18.2.0") to exercise the< 18.3.0gate.- Semver range strings (
^18.0.0,~18.0.0,>=17 <19, etc.) are interpreted loosely. eslint-plugin-react passes the value tosemver.satisfies, which throws on non-exact versions. rslint extracts the leading numeric triple (^18.0.0→18.0.0) and compares it directly. Prefer an exact version string for predictable behavior.