React rules
no-component-debounce
Section titled “no-component-debounce”Reports a call of debounce or throttle from lodash, lodash-es, or Underscore.js in the body of a React function component, a hook, or a render method, outside useMemo, useCallback, and a ref.
On in the recommended and all configs.
The component calls the function again on each render, so each render creates a new timer, and the debounced or throttled function never waits as intended.
What to do
Section titled “What to do”Move the call out of the component, or keep one function across renders with useMemo, useRef, or an instance property.
Example
Section titled “Example”function Search() { const onChange = debounce(search, 300); } reports function. Write const onChange = useMemo(() => debounce(search, 300), []).
no-generated-keys
Section titled “no-generated-keys”Reports a JSX key whose value calls Math.random() or Date.now(), alone or inside a template, a sum, String(), or .toString().
On in the recommended and all configs.
A key that changes on each render makes React drop and rebuild the element, which loses its state and focus and slows every render.
What to do
Section titled “What to do”Use a stable value that identifies the element, such as the id of the record it shows.
Example
Section titled “Example”<Row key={Math.random()} /> reports generated. Write <Row key={row.id} />.
no-leaked-jsx-number
Section titled “no-leaked-jsx-number”Reports the left side of && inside a JSX expression when its type is a number or a bigint, and also a string in files that import React Native.
On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.
React renders a falsy number such as 0 instead of nothing, and React Native fails on a string outside a Text element.
What to do
Section titled “What to do”Turn the condition into a boolean, such as count > 0 or Boolean(name).
Example
Section titled “Example”{count && <Badge />} with a number count reports leaked. Write {count > 0 && <Badge />}.
no-self-state-update
Section titled “no-self-state-update”Reports a call of a React useState setter whose only argument is the state variable of the same useState call.
On in the recommended and all configs.
Setting the state to the value it already holds changes nothing. The call usually stands where another value was meant.
What to do
Section titled “What to do”Pass the new value, or delete the call.
Example
Section titled “Example”const [count, setCount] = useState(0); setCount(count); reports useless. Write setCount(count + 1).
no-setter-in-component
Section titled “no-setter-in-component”Reports a call of a React useState setter in the body of the outermost component that declares the state, outside any if and any nested function. A component is a function whose name does not start with a lowercase letter.
On in the recommended and all configs.
Each state change renders the component again, and the body sets the state again on that render, so the component loops until React stops it.
What to do
Section titled “What to do”Set the state in an event handler, an effect, or an attribute callback, or compute the value during the render without state.
Example
Section titled “Example”function Cart() { const [count, setCount] = useState(0); setCount(1); } reports setter. Call setCount(1) from an event handler.
prefer-readonly-props
Section titled “prefer-readonly-props”Reports the typed props of a React function component whose properties are not all read-only.
On in the all config. It reports nothing without the type information of a TypeScript project.
React treats props as immutable. A read-only type lets the compiler reject a write to them.
What to do
Section titled “What to do”Wrap the type in Readonly, or mark each property readonly.
Example
Section titled “Example”function Badge(props: { label: string }) { return <b>{props.label}</b>; } reports mutable. Write props: Readonly<{ label: string }>.