Skip to content

Rule proposal: no-sync-state-usage-after-setstate #1648

Open
@dmose

Description

@dmose

From the React 16.2 docs:

setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.

In addition to this being effectively a race condition now, I would imagine that the race will get hit more often once React 16 allows for async rendering.

It would be wonderful to have a rule to flag this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions