logoESLint React
Rules

use-state

Full Name in @eslint-react/eslint-plugin

@eslint-react/naming-convention/use-state

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

react-naming-convention/use-state

Presets

recommended recommended-typescript recommended-type-checked strict strict-typescript strict-type-checked

Description

Enforces destructuring and symmetric naming of useState hook value and setter.

This rule ensures two things:

  1. The useState hook is destructured into a value and setter pair.
  2. The value and setter are named symmetrically (e.g. count and setCount).

Examples

Failing

import React, { useState } from "react";

function MyComponent() {
  const [count, updateFoo] = useState(0);
  //    ^^^^^^^^^^^^^^^^^^
  //    - The setter should be named 'set' followed by the capitalized state variable name, e.g., 'setState' for 'state'.

  return <div>{count}</div>;
}

Passing

import React, { useState } from "react";

function MyComponent() {
  const [count, setCount] = useState(0);

  return <div>{count}</div>;
}
import React, { useState } from "react";

function MyComponent() {
  const [{ foo, bar, baz }, setFooBarBaz] = useState({
    foo: "bbb",
    bar: "aaa",
    baz: "qqq",
  });

  return <div>{foo}</div>;
}

Rule Options

This rule accepts an optional configuration object with the following properties:

  • enforceSetterExistence (boolean, default: false): If set to true, the rule will enforce that a setter function is always present when destructuring the result of useState.
    const [value] = useState(0); // Error: Setter function is missing.

Implementation