Questions a beginner might ask #51
Replies: 2 comments 1 reply
-
✅ I'm adding "state transition" (and the follow up q) and "sub-tree basis" as terms to #46 |
Beta Was this translation helpful? Give feedback.
-
What is a state transition and how do we know if the state transition is expensive?Colloquially, a "state transition" is the same as a "state change". This is a state change: setState(something); Sometimes, setting state may lead to an "expensive" render. The way you can tell whether a render is "expensive" is by checking if the screen freezes. If you click on a button that sets state, and the button gets "stuck" for some time, and then gets "unstuck", this means that a bunch of code in your app had to run in between, and it took long enough for you to notice it. If React calling your components took longer than a single screen frame (~16ms) to run, the delay can be noticeable and hurt the user experience. (A slightly more technical way to figure out which state changes are expensive is by using a tool like a Profiler.) "State transition" is a colloquial term that's already in use, and React 18 builds on that usage to make it a formal concept in React. We don't often say "transition" in reference to something that's instant. We usually mean there's some kind of a delay. So this intuition matches how the React API is designed. If you mark the state update as a transition: startTransition(() => {
setState(something);
}) then React will know that it might take a bit of time, and will treat it differently. In particular, it will not let the browser get "stuck", and instead will periodically let it handle new events (like typing) and even interrupt this transition with other state updates. You might have thought of "transitions" as having something to do with animation. That's another common colloquial meaning. Today, For more on
|
Beta Was this translation helpful? Give feedback.
-
While going through the docs, I found some terminologies that I felt won't be familiar to people who are not very experienced with React. It would be helpful to break these down into simpler explanations -
useDeferredValue
→ How can we mark less important parts of the screen?Beta Was this translation helpful? Give feedback.
All reactions