no-set-state-in-component-will-update
Disallows calling 'this.setState' in 'componentWillUpdate' outside functions such as callbacks.
Full Name in eslint-plugin-react-x
react-x/no-set-state-in-component-will-updateFull Name in @eslint-react/eslint-plugin
@eslint-react/no-set-state-in-component-will-updatePresets
x
recommended
recommended-typescript
recommended-type-checked
strict
strict-typescript
strict-type-checked
Rule Details
Updating the state in componentWillUpdate is not allowed because the component is already about to re-render. It can lead to infinite loops and inconsistent state.
Examples
Calling setState directly in componentWillUpdate
// Problem: Calling setState directly in componentWillUpdate triggers another render and can easily cause infinite loops or layout thrashing
import React from "react";
interface MyComponentProps {}
interface MyComponentState {
name: string;
}
class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
componentWillUpdate() {
this.setState({ name: "John" });
// ^^^ Do not call `this.setState` in `componentWillUpdate` outside functions such as callbacks.
}
render() {
return <div>Hello {this.state.name}</div>;
}
}Preparing for an update without setState
If you need to compute something before an update, store it in an instance variable or derive it during render. Never call setState in componentWillUpdate because the component is already about to re-render.
// Recommended: Use instance variables or derive values during render
import React from "react";
interface MyComponentProps {
value: number;
}
interface MyComponentState {
prevValue: number;
}
class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
state = {
prevValue: 0,
};
componentWillUpdate(nextProps: MyComponentProps) {
// Store previous value in an instance variable, not state
this.previousValue = this.props.value;
}
render() {
const hasIncreased = this.props.value > this.state.prevValue;
return <div>Value increased: {hasIncreased ? "Yes" : "No"}</div>;
}
}Versions
Resources
Further Reading
- Legacy React APIs:
setState - Legacy React APIs:
componentWillUpdate - React Docs: How to migrate to a function component
See Also
react-x/no-set-state-in-component-did-mount
Disallows callingthis.setStateincomponentDidMountoutside functions such as callbacks.react-x/no-set-state-in-component-did-update
Disallows callingthis.setStateincomponentDidUpdateoutside functions such as callbacks.