>will block rendering and will defer to a parent component to render instead?
Yes, but only after the delay period passes. Before that, nothing will happen on the screen (at least not until I add an inline spinner as one of the last steps). Preventing a spinner from appearing and instantly disappearing on fast connections is exactly what this feature enables.
>Interesting magic I'd say, since I wouldn't expect such a line to block my rendering.
It works by throwing a Promise (and React catches it and waits for it before retrying rendering). So it’s not really magic, just an unfamiliar pattern. Conceptually this is similar to algebraic effects in languages that have them.
>Wouldn't it be a more "React way" to have HOC component or a render prop that would work exactly like this .read() API?
No. If you watch the demo closely you’ll notice we don’t show the spinner if the load is fast enough. React prevents updating the whole tree. This is impossible to express with a HOC without reimplementing the whole React in it.
>There's lots of magic, especially in the IO part, and would've loved if he went more into detail on how it's actually done - it's a JS-conf afterall.
As I said earlier the actual
mechanism is quite straightforward. We throw and later React retries.
We’ll be posting an RFC with details. But for now we felt it was important to motivate this work, and even the motivation barely fit into 30 minutes.
>Observables might be a more interesting abstraction for IO-kind of things.
The core purpose of this feature is to suspend rendering until data is ready to avoid flicker and things jumping around. You can use observables in your code but I’d argue that, save for a few use cases, having components jump around as new items “come in” is a janky experience. You can always build a Promise interface that coalesces those observable updates into Promises of a final list before rendering.
>Also, testing this seems rather complex since the render is effectively doing a side-effect.
I actually think testing will get easier because now you can wrap your tree with a fake cache in tests. Then even components that do data fetching won’t attempt to reach the real network.
>Also, how's race-conditioning done? Does the fetcher library have the logic to only render the last fetcher, or is it more complicated than that?
The fetcher library doesn’t have much logic. It’s basically a cache backed by two Maps (for pending and resolved data).
React just retries rendering after Promise has resolved (or re-render happened due to another reason, like prop change). So there is no need for explicit logic handling race conditions. Instead of keeping continuations “in flight” we just abort rendering and retry when data is ready. So there’s nothing to race for.