Skip to content

Understanding act behaviour #15543

@mhuebert

Description

@mhuebert

I have been trying to use act for the first time, and having some issues, and so I'm wondering if my expectations are wrong about what it is supposed to do, or if I am "doing it wrong".

What is the current behavior?

The only way I can observe the results of state changes I initiate is by using a timeout.

Paste the link to your JSFiddle or CodeSandbox example below:

https://codesandbox.io/s/k5zmln6w83?expanddevtools=1&fontsize=14&hidenavigation=1

What is the expected behavior?

What I expect is that by wrapping a state change or render operation in act, all of the resulting state changes / side-effects / re-renders will be complete by the time act returns, so that the operation appears (or is coerced to be) synchronous.

I created an example (https://codesandbox.io/s/k5zmln6w83?expanddevtools=1&fontsize=14&hidenavigation=1) wherein I render a view via unstable_ConcurrentMode. In the view, I create a useState hook with a value of 0. After the view is rendered, I use that hook's setter to change its state to 1.

Below is a log of the steps I take, showing three values at each time: seenByRender, the last state-value that appeared within the render body; calculated, the last value returned from my state-update function; and seenByEffect, the last value observed from a useEffect I create in the view.

1. before act/render:   seenByRender: null, calculated: null, seenByEffect: null
2. after act/render:    seenByRender: 0,    calculated: null, seenByEffect: 0
3. ---incrementing with act/setState---
4. after act/increment: seenByRender: 0,    calculated: 1,    seenByEffect: 0
5. after timeout:       seenByRender: 1,    calculated: 1,    seenByEffect: 1

What I am wanting/expecting is for step 4 to look like step 5, ie, I can somehow test the full consequences of my setState call.

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

16.8.6, using unstable_ConcurrentMode

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