-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State updates outside of act
interfere with subsequent state updates during act
#1794
Comments
act
calls can prevent state updates inside of act
calls from executing synchronouslyact
interfere with subsequent state updates during act
As far as I know it is because your first render should also be incorporated into We changed Works fine when I change it to:
|
A conceptually very similar issue exists with Line 235 in ea1e12d
|
In the context that I extracted this test case from, the state update is triggered asynchronously inside a |
One possible solution might be that an |
@robertknight I wonder if that behavior could be the default for |
When scheduling a state update or effect, check whether the `options.{debounceRendering, requestAnimationFrame}` hooks changed since an update/effect queue flush was last scheduled and schedule a flush using the new hooks if so. This fixes an issue where a call to `act` would fail to synchronously flush state updates and effects if a flush was already scheduled but not yet executed before `act` was called. This happened because when a state update or effect was queued during the `act` callback, if the global update/effect queues were non-empty then the temporary hooks installed by `act` to facilitate synchronously flushing the queue would not be called. More generally, this fixes an issue where any changes to the scheduling hooks do not take effect until any already-scheduled flushes have happened. Fixes #1794
When scheduling a state update or effect, check whether the `options.{debounceRendering, requestAnimationFrame}` hooks changed since an update/effect queue flush was last scheduled and schedule a flush using the new hooks if so. This fixes an issue where a call to `act` would fail to synchronously flush state updates and effects if a flush was already scheduled but not yet executed before `act` was called. This happened because when a state update or effect was queued during the `act` callback, if the global update/effect queues were non-empty then the temporary hooks installed by `act` to facilitate synchronously flushing the queue would not be called. More generally, this fixes an issue where any changes to the scheduling hooks do not take effect until any already-scheduled flushes have happened. Fixes #1794
On reflection I realized that there was a more general issue here to do with the way the scheduling hooks ( |
This should fix a cause of flakey tests [1] [1] preactjs/preact#1794
This should fix a cause of flakey tests [1] [1] preactjs/preact#1794
A state update to a component scheduled outside of an
act()
callback can prevent a state update scheduled inside of anact()
callback from executing synchronously as it should:Steps to reproduce:
Runnable demo: https://codesandbox.io/s/preact-10-state-update-after-unmount-y1fj0
nb. With this sandbox I see different outputs in the DOM when viewing the sandbox output in an iframe vs a new window. In the iframe the DOM output matches the console log. In a new window, the DOM updates after the console log to show "2".
Expected output:
Actual output:
Notes:
When
setState
is called or a state update hook runs, theenqueueRender
function is called. This function uses theoptions.debounceRendering
hook which is overridden in the context of anact
call. However,options.debounceRendering
is only called if the deferred rendering queue is empty. In the above example, when the component is updated inside theact
call, the queue is non-empty so rendering is deferred.The above example was extracted from a test where a state update was being triggered in a timeout, outside of an
act
call, to work around an issue with CSS transitions in certain browsers. This surprisingly interfered with later tests due to the global deferred render queue.The text was updated successfully, but these errors were encountered: