DocumentationRulesno-create-ref

no-create-ref

Full Name in eslint-plugin-react-x

react-x/no-create-ref

Full Name in @eslint-react/eslint-plugin

@eslint-react/no-create-ref

Features

🔍

Presets

  • core
  • recommended
  • recommended-typescript
  • recommended-type-checked

What it does

Prevents usage of createRef() in function components.

Why is this bad?

createRef() is a legacy API that is not recommended for use in new code. Instead, prefer using useRef() with function components.

Examples

Failing

import React, { createRef } from "react";
 
function Example() {
  const ref = React.createRef<HTMLDivElement>();
  //          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  //          - [Deprecated] Use 'useRef' instead.
 
  return <div ref={ref} />;
}

Passing

import React, { useRef } from "react";
 
function Example() {
  const ref = useRef<HTMLDivElement>(null);
 
  return <div ref={ref} />;
}
import React, { createRef } from "react";
 
class Example extends React.Component {
  inputRef = createRef();
  // ...
}

Further Reading