Zum Inhalt springen

State Set During Render (ARCH3305)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key typescript.react.set-state-during-render · Severity error · Category quality

React: flags a state setter called in the component body. Each call schedules another render, which calls it again.

function Cart({ lines }) {
const [total, setTotal] = useState(0);
setTotal(sum(lines));
return <span>{total}</span>;
}
function Cart({ lines }) {
const total = useMemo(() => sum(lines), [lines]);
return <span>{total}</span>;
}