Do you ever wonder how React decides which task to work on first?
Do you ever wonder how React decides which task to work on first? How does it decide the priority of each task? Like, two things happen in the application. First, a user types into a search bo
Do you ever wonder how React decides which task to work on first?
How does it decide the priority of each task?
Like, two things happen in the application.
First, a user types into a search box and expects immediate results.
Second, a large list of search results needs to be rendered.
If React processes the large list first, the main thread may be blocked
during that time, making the application feel slow while the user types
in the search box.
This degrades the user experience.
How does React decide which task to perform first?
And what happens when one task is being performed and a higher-priority
task comes in?
For this, React has a scheduler.
React's scheduling architecture helps decide when rendering work is
performed and which updates should receive priority.
Imagine two tasks are pending:
First, a low-priority task, and second, a high-priority task.
Now, which task is performed first?
The second one, right?
Because it has a higher priority.
So, the scheduler picks the second task for execution.
Now, the question is:
What happens when one task is being executed and another,
higher-priority task comes in?
React has a concurrent rendering feature that helps interrupt or
restart eligible rendering work.
This means React can pause the current work and start working on the
higher-priority task.
After executing it, React can continue or restart the previous
rendering work.
Now, I have one question for you:
In which case does React start the rendering process from the beginning
instead of continuing?
React's scheduler doesn't make a React application faster, but it
decides which task to perform first so that the user experience
improves by keeping the application responsive while React manages
rendering work.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.