no-string-refs
Added in v0.5.0Configuration
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,settings.react.defaultVersionis used, then latest.settings.react.pragma— used to recognize<pragma>.createClass(...)and classes extending<pragma>.Component/<pragma>.PureComponent. Defaults toReact; a file-level@jsxannotation takes precedence.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 fromnode_modules. rslint usessettings.react.defaultVersionwhen provided and otherwise treats the version as latest. Set an explicit version string (e.g."18.2.0") for exact version-aware behavior.- Semver range strings (
^18.0.0,~18.0.0,>=17 <19, etc.) are interpreted differently. eslint-plugin-react coerces the setting to one version before comparison, while rslint extracts the first numeric triple and compares it directly. Prefer an exact version string for predictable behavior. - Computed identifier member names are not treated as literal property names. eslint-plugin-react reads ESTree's
property.namewithout checkingcomputed, so it reportsthis[refs]and classifiesReact[createClass](...)when those identifiers happen to have the configured names. rslint does not assume their runtime values. Privatethis.#refsis likewise not the public legacythis.refsAPI and is not reported. - Invalid
settings.react.createClassvalues do not terminate linting. eslint-plugin-react throws while initializing the rule; rslint treats the configured string as a non-matching factory name because the native rule API has no recoverable settings-error channel.