Preact/X/Replace This State With Prevstate

1.0.0Last update Aug 21, 2024
by@manishjha-04

A codemod that prevents synchronous state access in Preact by converting setState to use a callback function.

Detailed description:
In Preact X, state updates are no longer applied synchronously, meaning that accessing this.state immediately after calling setState may return outdated values. This codemod refactors your code to use the functional form of setState, ensuring that state updates correctly depend on the previous state, preventing potential bugs and ensuring compatibility with Preact X.

Example

Before

this.setState({ counter: this.state.counter + 1 });

After

this.setState((prevState) => {
return { counter: prevState.counter + 1 };
});

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now