Dan Profile picture
12 Jun, 29 tweets, 8 min read
It's another Friday night indoors, so being the nerd I am, of course I am playing with adding startTransition to open source apps to see if it works. Don't judge me
We really need to make some demos but maybe I could record a few short videos if y'all are curious (but no FOMO please, it's still an Alpha, etc)
Okay so this one shouldn't be hard to follow. There's an input and a table. Currently, table state update is delayed and debounced so it doesn't stutter typing. But stutter is noticeable if I type slower. (Caveat: we're in DEV mode so the real app is faster—but nice for demo.)
I wonder what happens if we remove debouncing and just update the table on every input update? OUCH. That's why it's debounced. Makes sense.
Now let's switch the app to createRoot() and add this little friend around the table update: github.com/reactwg/react-…   const handleInputChange =...
This is the result. Literally no other changes to the app code. Unlike the debounced version, it starts handling the input immediately. But it also doesn't lock up the browser to re-render a table because that update is marked as a transition. Typing again interrupts it.
I hope we can put the "this is only useful to Facebook" argument to rest right here.
The cool thing about this is that it adapts to the computer.

On a fast computer, it responds instantly because it re-renders the table between keystrokes. So no unnecessary delay like debouncing.

On a slow computer, table update "catches up" but the input stutters much less.
It wasn't easy to find an app to test it on because larger examples often use state managers. While we'll definitely make state managers compatible with 18 (ongoing work!), this feature is unlocked by using... plain React state. I hope it'll let you use state in more places.
But unlike our previous strategy, there are no big global changes you have to make. No scary "mode" to prepare your app for.

Need to keep using a state manager? Keep using it! But you can move a bit of UI state back into React and then try startTransition for that part of it.
Oh, this UI looks pretty cool. Seems like it's fast even without startTransition. Nothing to do here. Or maybe?...
Would be a shame if something happened to it... Like if we slow it down 4x to simulate a slow device. You can optimize your code, but if all updates are urgent, at some point synchronous rendering won't fit in a single frame. Hello, jank.
You could write fancy coordination code to manage animation frames and throttle chart rendering. That could be a project to show off at next sprint. Or you can just throw in startTransition in there. Still 4x slowed down! The chart "catches up" slower but the input doesn't lag.
Again, on a fast computer, it's just fast. On a slow one, it "catches up". Of course you could still add throttling or debouncing if you wanted to. But why would you?
One thing that feels a bit annoying is that on a slow device, there's no visual feedback that something is happening. So let's replace startTransition with useTransition, which gives us back the isPending flag:   const [minScore, setMinSc...
I put a small delay on the CSS transition, so on a fast device isPending immediately goes from true to false, and we don't show anything special. But if it stays true for a while, the CSS transition kicks in, and we see the pending chart dimmed to reflect its content is stale!
Actually, why not use the same strategy in the filter table example? Although the table state lives "above" the input, it's the input that wraps a parent call into a transition. So this input can display the visual feedback regarding its progress. No manual coordination! function GlobalFilter({ onG...
So now if the device is slow, we can show a pending indicator... in the input... that reflects whether the parent table re-render in progress... while you can still type at any point without stutter. Thanks, concurrency!

Five lines of code.
That 6x slowdown (with additional ~3-5x slowdown due to DEV mode) is nasty. Watching that spinner gets a bit tiring! But just for fun, let’s run the exact same code in today’s React without startTransition.

Yeah.

Imagine going back to this.
It’s great when you can “just make it faster” or “optimize rendering”. But with other things being equal, it’s just better for non-critical updates be interruptible. On a slow device, it matters. It’s common sense.

We want React users to have that option. With 2-5 lines of code.
The irony is that we haven’t even optimized for this “slow rendering” use case. We left many optimizations until after React 18 because we were focused on the async use case. (The same startTransition API also integrates with data fetching.) But that’s a story for another day.
I want to give a shoutout to @acdlite for doing a vast amount of technical work on these features over the past five years. He’s been thinking about this nonstop so that you can just plug it in. We can’t wait for you to be able to.
I hope this gives you a glimpse of what we’ve been working on. You can see that it actually *works*. It’s not a demo anymore. It’s real. But yes, there will be challenges. We’ll work with the ecosystem maintainers to fix library issues. We’ll be filling in the gaps on our side.
To ship these features, we’ll need your patience and your help. We will work with a few projects first via the React 18 Working Group to determine our guidance and any gaps in APIs for libraries. We’ll need your help spreading that knowledge later. github.com/reactwg/react-…
It’s been a long journey. Stick with us for a little bit longer. We’re in a final phase now. But it’s also a new beginning. The examples above are just small optimizations compared to the actual features we can build on this new foundation. Concurrent rendering is awesome.
PS. One thing you might’ve noticed is that when slowed down, the chart “waits” for me to stop messing with the input. We used to have an optimization to prevent this, but removed it to fix bugs. We’ll re-add it someday. Then it won’t need to wait and will be even more responsive.
If you remember our very first 2017 “triangle demo” (which was widely misunderstood because it was incomprehensible) — that’s what it meant to show. That it’s able to perform a “slow” update (numbers incrementing) despite being interrupted by “fast” updates (animating scale).
I think the React repo might still have that demo lying somewhere. I’ve seen it copied and used as example that some other library can render a moving triangle faster. But that missed the point because the demo was artificially slowed down to simulate a deep tree with user code.
The irony is that our very first demo won’t work in React 18. It’s because of that missing optimization that’s necessary to “resume” interrupted work instead of starting from scratch. We’ll get there but just allowing rendering it be interrupted is such an improvement by itself.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Dan

Dan Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @dan_abramov

10 Jun
@DavidKPiano @acemarke @modernserf I’ve tried to be more precise with my wording here—does this help? I didn’t see my tweet as a criticism but maybe I have misread the room.
@DavidKPiano @acemarke @modernserf My point is that, I don’t agree with the premise of rejecting tools based on them being complex inside. Requirements of modern client apps are complex! So that complexity has to live somewhere. I know doesn’t live inside Redux because I literally wrote it. I can’t say this?
@DavidKPiano @acemarke @modernserf This complexity could live in application code, it could live in a meta-library on top of Redux, or somewhere else. I just definitely know that it’s not in those 100 lines of code. And that’s okay. It’s not a criticism. It is a direct reflection of what Redux is (and isn’t).
Read 4 tweets
10 Jun
Love this thread. Someday I’d like to write (or contribute to) a five year retrospective of this project. So many stories! For now I’ll do a small thread of random fun moments I remember from the last couple of years.
When we started, we wrote our own scheduler with a plan that eventually the browser would provide a native one. Our work on React took long enough that by this year, the native scheduler already exists behind a flag and we could even experiment with it! chromestatus.com/feature/603116…
Our first thinking about how Suspense should work didn’t make sense. We found out the hard way in the middle of the FB website rewrite in 2019. A simple change could make UI stall for 5 seconds and engineers couldn’t say why. We couldn’t either. We were confused by our own API!
Read 32 tweets
11 Apr
Looking for a very particular kind of *instrumental* music. Needs to be a bit non-idiomatic, kaleidoscopic in texture. But with beautiful ethereal moments of harmony when things come together. Some of Four Tet, Boards of Canada, Bibio stuff is like this. Give me more.
References Bibio — YouBoards of Canada — Dayvan CowboyFour Tet — Angel EchoesMaribou State — Feel Good
Would be cool if went into a more math rock direction but still beautiful, not just technical
Read 5 tweets
10 Apr
What are your favorite songs in 3/4? (Meaning waltz type beat: one-two-three, one-two-three...) Spotify or YouTube links pls.
Tom Waits — Widow’s Grove. No idea what it’s about but so pretty. I think the music itself sounds like a riff on some traditional folk melody but don’t know the lineage.
БГ — Елизавета. This one is in Russian. It gives me a similar feeling as Widow’s Grove that despite the lyrics being abstract and obscure, I feel exactly what it says.
Read 13 tweets
23 Mar
I’ve lived 80% of my life in Russia and 20% in the UK. Both countries have some strange or unpleasant aspects. (For example, one of them poisons its own citizens with a nerve agent.)

Neither of them has news reports every other week about some random person shooting up a store.
The most fascinating to me is that while there seems to be majority support for stricter laws, there’s also majority opposition to e.g. outright banning handguns. I’m perplexed as to what kind of experiences one needs to have to be convinced that weapons with bullets are needed.
I know some people say guns are their hobby. My thinking process is, like, maybe you could pick a different hobby? How about making bombs, is that a reasonable hobby too?

Though a designated park where people who like to shoot can hang out with each other seems like an OK idea.
Read 18 tweets
20 Mar
Every codebase eventually grows technical debt. Requirements change, things that used to make sense no longer don’t. People who came up with them aren’t around anymore.

When tech people say our world isn’t deeply messed up, I’m confused. It’s the biggest legacy system out there.
This piece is spot-on. So many harms of today trace back to stupid ideas from hundreds of years ago. Even if most people aren’t that violent, these ideas of “purity” and “modesty” and “shame” are ingrained in our societies. We get so used we don’t notice. religiondispatches.org/dont-discount-…
The same goes for “meritocracy” or capitalism. If you found a way to live within that system, and the system didn’t break you, it is tempting to think of it as a law of nature. Like gravity. But if you wouldn’t risk swapping places with someone else, are you sure that it works?
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(