Skip to content

Calling multiple setState's inside a timeout callback causes multiple rerenders #14837

Closed
@lucasterra

Description

@lucasterra

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?
Calling setState inside a timeout callback causes multiple rerenders.

Here is a demo showing this:
https://codesandbox.io/s/w710184o8w

On component mount, I'm making it fire a 1000ms timeout and calling multiple setState. I have a counter to count the amount of rerenders. The button also has another callback, which fires onClick, also calling the same setState. The set timeout callback causes 4 rerenders, while the onClick callback only causes a single rerender, as expected.

This is probably not specific to setTimeout, but this is the simplest way I found to reproduce this problem.

What is the expected behavior?
I expected the setTimeout callback to also cause a single rerender only.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.8+

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions