Skip to content

React rules

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.

Move the call out of the component, or keep one function across renders with useMemo, useRef, or an instance property.

function Search() { const onChange = debounce(search, 300); } reports function. Write const onChange = useMemo(() => debounce(search, 300), []).

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.

Use a stable value that identifies the element, such as the id of the record it shows.

<Row key={Math.random()} /> reports generated. Write <Row key={row.id} />.

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.

Turn the condition into a boolean, such as count > 0 or Boolean(name).

{count && <Badge />} with a number count reports leaked. Write {count > 0 && <Badge />}.

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.

Pass the new value, or delete the call.

const [count, setCount] = useState(0); setCount(count); reports useless. Write setCount(count + 1).

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.

Set the state in an event handler, an effect, or an attribute callback, or compute the value during the render without state.

function Cart() { const [count, setCount] = useState(0); setCount(1); } reports setter. Call setCount(1) from an event handler.

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.

Wrap the type in Readonly, or mark each property readonly.

function Badge(props: { label: string }) { return <b>{props.label}</b>; } reports mutable. Write props: Readonly<{ label: string }>.