h14h 5 days ago

Excellent read! This is the first time I feel like I finally have a good handle on the "what" & "why" of RSCs.

It has also sparked a strong desire to see RSCs compared and contrasted with Phoenix LiveView.

The distinction between RSCs sending "JSX" over the Wire, and LiveViews sending "minimal HTML diffs"[0] over the wire is fascinating to me, and I'm really curious how the two methodologies compare/contrast in practice.

It'd be especially interesting to see how client-driven mutations are handled under each paradigm. For example, let's say an "onClick" is added to the `<button>` element in the `LikeButton` client component -- it immediately brings up a laundry list of questions for me:

1. Do you update the client state optimistically? 2. If you do, what do you do if the server request fails? 3. If you don't, what do you do instead? Intermediate loading state? 4. What happens if some of your friends submit likes the same time you do? 5. What if a user accidentally "liked", and tries to immediately "unlike" by double-clicking? 6. What if a friend submitted a like right after you did, but theirs was persisted before yours?

(I'll refrain from adding questions about how all this would work in a globally distributed system (like BlueSky) with multiple servers and DB replicas ;))

Essentially, I'm curious whether RSCs offer potential solutions to the same sorts of problems Jose Valim identified here[1] when looking at Remix Submission & Revalidation.

Overall, LiveView & RSCs are easily my top two most exciting "full stack" application frameworks, and I love seeing how radically different their approaches are to solving the same set of problems.

[0]: <https://www.phoenixframework.org/blog/phoenix-liveview-1.0-r...> [1]: <https://dashbit.co/blog/remix-concurrent-submissions-flawed>

2
rwieruch 5 days ago

I have used RSCs only in Next.js, but to answer your questions:

1./2.: You can update it optimistically. [0]

3.: Depends on the framework's implementation. In Next.js, you'd invalidate the cache. [1][2]

4.: In the case of the like button, it would be a "form button" [3] which would have different ways [4] to show a pending state. It can be done with useFormStatus, useTransition or useActionState depending on your other needs in this component.

5.: You block the double request with useTransition [5] to disable the button.

6.: In Next, you would invalidate the cache and would see your like and the like of the other user.

[0] https://react.dev/reference/react/useOptimistic

[1] https://nextjs.org/docs/app/api-reference/functions/revalida...

[2] https://nextjs.org/docs/app/api-reference/directives/use-cac...

[3] https://www.robinwieruch.de/react-form-button/

[4] https://www.robinwieruch.de/react-form-loading-pending-actio...

[5] https://react.dev/reference/react/useTransition

sophiebits 5 days ago

React offers a useOptimistic Hook that is designed for client-side optimistic updates and automatically handles reverting the update upon failure, etc: https://react.dev/reference/react/useOptimistic