Skip to content
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

Bug: Why does startTransition cause two renders instead of one? #24269

Closed
zszs123 opened this issue Apr 3, 2022 · 2 comments
Closed

Bug: Why does startTransition cause two renders instead of one? #24269

zszs123 opened this issue Apr 3, 2022 · 2 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@zszs123
Copy link

zszs123 commented Apr 3, 2022

React version: 18

Steps To Reproduce

  1. you can open the url:https://codesandbox.io/s/clever-albattani-zmpxh1?file=/src/App.js
  2. then you can see, in the useEffect,I set a five-second delay,then setState.
  3. I used useTransition to use concurrent updates,only once.
  4. but the app component update twice ,you can see the view change twice.

4b0ef921ac260f9c63f0be7d167bbaf

  1. this is the image of performance record, you can see the first upadate is sync ,and the second update is conCurrent, why update twice but I setState once.

Link to code example:

The current behavior

The expected behavior

@zszs123 zszs123 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 3, 2022
@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2022

When you run a transition, it schedules two renders:

  1. One urgent render to show a pending state (isPending = true) with old state value
  2. One concurrent render with isPending = false and new state value
    a. When this render is completed, it gets committed

To make the urgent render fast, you can memoize that component that changes:

https://codesandbox.io/s/lively-cookies-rghnzn?file=/src/App.js

There's still a noticeable frame drop during the commit. I think that's just due to the number of DOM elements being inserted.

@gaearon
Copy link
Collaborator

gaearon commented Apr 3, 2022

Here's an adjusted version where each item actually has a forced rendering delay (your empty loop wasn't very representative because the engine probably optimized it away).

https://codesandbox.io/s/brave-haze-i7wm7v?file=/src/App.js

Try removing startTransition and you'll notice how the difference is a lot more noticeable.

@gaearon gaearon closed this as completed Apr 3, 2022
@gaearon gaearon changed the title Bug: When I use conCurrent, the App component update twice, you can see the view change twice,but I only change state once. Bug: Why does startTransition cause two renders instead of one? Apr 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants