logoESLint React

ref-name

Enforces identifier names assigned from 'useRef' calls to be either 'ref' or end with 'Ref'.

Full Name in eslint-plugin-react-naming-convention

react-naming-convention/ref-name

Full Name in @eslint-react/eslint-plugin

@eslint-react/naming-convention-ref-name

Presets

naming-convention recommended recommended-typescript recommended-type-checked strict strict-typescript strict-type-checked

Rule Details

Enforces identifier names assigned from useRef calls to be either ref or end with Ref.

Examples

// 🔴 Problem: Identifier 'myId' does not end with 'Ref'
const myId = useRef(null);
// 🔴 Problem: Identifier 'node' does not end with 'Ref'
const node = useRef(null);
// 🟢 Recommended: Name ends with 'Ref'
const myRef = useRef(null);
// 🟢 Recommended: Name is exactly 'ref'
const ref = useRef(null);
// 🔵 OK: Name ends with 'Ref' (MemberExpression)
obj.nested.myRef = useRef(null);
// 🔵 OK: Not reported by this rule — the result is immediately accessed via a member expression instead of being stored in a ref variable
// (note: reading `.current` during rendering like this is discouraged by the React docs, except for initialization)
const value = useRef(null).current.value;
// 🔵 OK: Not reported by this rule — same as above (the `??=` lazy-initialization write is the officially tolerated exception)
const useOnce = <T,>(fn: () => T) => (useRef<{ value: T }>().current ??= { value: fn() }).value;

Options

Shared Settings

Custom ref hooks can be configured via shared ESLint settings under react-x. The value must be a JavaScript RegExp string with delimiters and flags (see Configure Analyzer).

{
  "settings": {
    "react-x": {
      "additionalRefHooks": "/^(useMyRef|useCustomRef)$/u"
    }
  }
}

Resources

Further Reading


See Also

On this page