Rules
no-create-ref
Disallows 'createRef' in function components.
Full Name in eslint-plugin-react-x
react-x/no-create-refFull Name in @eslint-react/eslint-plugin
@eslint-react/no-create-refPresets
x
recommended
recommended-typescript
recommended-type-checked
strict
strict-typescript
strict-type-checked
Rule Details
createRef() is a legacy API that is not recommended for use in new code. Instead, prefer using useRef() with function components.
Common Violations
Invalid
import React, { createRef } from "react";
function MyComponent() {
const ref = React.createRef<HTMLDivElement>();
// ^^^ [Deprecated] Use 'useRef' instead.
return <div ref={ref} />;
}Valid
import React, { useRef } from "react";
function MyComponent() {
const ref = useRef<HTMLDivElement>(null);
return <div ref={ref} />;
}import React, { createRef } from "react";
class MyComponent extends React.Component {
inputRef = createRef();
// ...
}Resources
Further Reading
See Also
react-naming-convention/ref-name
Enforces identifier names assigned fromuseRefcalls to be eitherrefor end withRef.react-x/no-forward-ref
Replaces usage offorwardRefwith passingrefas a prop.react-x/refs
Validates correct usage of refs by checking thatref.currentis not read or written during render.