M

preact/X/replace-this-state-with-prevstate

How to Use
Run this codemod on your codebase using one of the following commands

The easiest way to run this codemod without installing anything globally:

Documentation

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

ts

After

ts

Ready to contribute?

Build your own codemod and share it with the community.