Rules
use-state
Full Name in @eslint-react/eslint-plugin
@eslint-react/naming-convention/use-stateFull Name in eslint-plugin-react-naming-convention
react-naming-convention/use-statePresets
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:
- The
useStatehook is destructured into a value and setter pair. - The value and setter are named symmetrically (e.g.
countandsetCount).
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 totrue, the rule will enforce that a setter function is always present when destructuring the result ofuseState.const [value] = useState(0); // Error: Setter function is missing.