no-string-refs
Rule category
Restriction.
What it does
Disallows using deprecated string refs.
Why is this bad?
String refs are deprecated in React. Use callback refs instead.
Examples
Failing
import React from "react";
function function Example(): React.JSX.ElementExample() {
return <JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div React.RefAttributes<HTMLDivElement>.ref?: React.LegacyRef<HTMLDivElement> | undefinedAllows getting a ref to the component instance.
Once the component unmounts, React will set `ref.current` to `null`
(or call the ref with `null` if you passed a callback ref).ref="ref" />;
// - String refs are deprecated. Use callback refs instead.
}Passing
import React, { function useRef<T>(initialValue: T): React.MutableRefObject<T> (+2 overloads)`useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
(`initialValue`). The returned object will persist for the full lifetime of the component.
Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
value around similar to how you’d use instance fields in classes.useRef } from "react";
function function Example(): React.JSX.ElementExample() {
const const ref: React.RefObject<HTMLDivElement>ref = useRef<HTMLDivElement>(initialValue: HTMLDivElement | null): React.RefObject<HTMLDivElement> (+2 overloads)`useRef` returns a mutable ref object whose `.current` property is initialized to the passed argument
(`initialValue`). The returned object will persist for the full lifetime of the component.
Note that `useRef()` is useful for more than the `ref` attribute. It’s handy for keeping any mutable
value around similar to how you’d use instance fields in classes.
Usage note: if you need the result of useRef to be directly mutable, include `| null` in the type
of the generic argument.useRef<HTMLDivElement>(null);
return <JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div React.RefAttributes<HTMLDivElement>.ref?: React.LegacyRef<HTMLDivElement> | undefinedAllows getting a ref to the component instance.
Once the component unmounts, React will set `ref.current` to `null`
(or call the ref with `null` if you passed a callback ref).ref={const ref: React.RefObject<HTMLDivElement>ref} />;
}