GabrielWilliamson

GabrielWilliamson

Using React components in Phoenix LiveView

Hi, after experimenting a bit, I wrote a short article and did a mini-demo of an approach I find interesting: a combination of tools like React and Phoenix. I’d like to know your opinion on this approach. Thanks.

https://gabrielwilliamson.com/posts/using-react-components-in-phoenix-live-view

First 10 of 10 Posts Switch mode

krasenyp

krasenyp

Good exploration but a better approach, in my opinion, is using a web component. It can be integrated with LiveView better and also forms and so on. Nowadays there’s a myriad of browser APIs which are easy to use from a web component.

DaAnalyst

DaAnalyst

Absolutely! Web components + Lit (+ a couple of extra directives) and there’s nothing you can’t do.
We’ve just started switching from Alpine to web components and it’s a bliss!

krasenyp

krasenyp

Wouldn’t call it a bliss but it’s good enough. I say it as a veteran front-ender.

DaAnalyst

DaAnalyst

It’s a bliss compared to Alpine (and its integration with LiveView)

kokolegorille

kokolegorille

I have written web component, with React…

I am not sure they address the same problem, and they are not mutually exclusive.

State management is something I miss with web components.

DaAnalyst

DaAnalyst

What exactly do you mean by this? We’re primarily using web components because they can manage their state.

kokolegorille

kokolegorille

I meant communication inter web components… sorry, not state management, but components communications

DaAnalyst

DaAnalyst

I see.

As a general pattern they promote parent → child communication via properties and child to parent via custom events. However, the former is only applicable when the composition is done within the web components themselves.

In our case (LiveView, where composition is achieved with LiveComponents) my take is the communication between web components need to be designed differently. First, if LiveView is the source of ultimate/eventual truth for the state that is also present server-side, for that part the rendering (or push_event) should have the last say at all levels of nesting. As for a pure client-side state and the communication thereof, the pattern is more like this:

  • parent → children: custom event on window with the children listening
  • child → parent: el.closest( ‘my-parent-web-component’).gimmeWhatIWant() // way faster than custom events

The principal difference is in the underlying intent. Generally, web components are meant to be reusable, universal and isolated which is why they promote loose coupling. In our case with LiveView, where we use web components within a predetermined DOM hierarchy within which they role is to implement the client-side state and behavior, we only need resort to events so we can benefit from their mechanics and no longer to promote a loose coupling (unless, of course, a particular web component is truly generic).

kokolegorille

kokolegorille

At the time the recommended solution was to use an event bus.

But my use case was a rich video player…

Liveview manage props, but all the rest is client side.

I wrote a web component for portability, hiding a react interface. That is why I wanted to say it’s not exclusive.

The bonus, it’s also standalone and can use Phoenix channels, it does not need to be hosted on a Phoenix server.

DaAnalyst

DaAnalyst

The “event bus” is merely a wrapper for dispatching custom events. If performance is a concern and you only need to notify or fetch data from a single specific web component it’s almost always better to do it directly, especially if there’s no need for loose coupling (e.g. when a child knows upfront what class/web component is its target parent). But you can do it whatever way it best suits your needs.

— All posts loaded —

Where Next?

Trending in Blog Posts Top

bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
ryanzidago
Hi all, In this article, I make the case for each test owning its setup. Usually I forbid my AI agents to use the setup callbacks; I mu...
New
zorn
As I’ve leaned into AI code generation on LocalCents, the volume I ship has climbed, and my worry shifted from any single change to the l...
New
jswanner
I wrote about an issue I had with a LiveView application, and how I solved the problem by debouncing updates server-side (within the Live...
New
abreujp
I’ve published a new article in my Elixir learning series on dev.to exploring what happens when tagged tuples aren’t enough - the try, re...
New
pckrishnadas88
This article demonstrates how to build a minimal stateful process using only Elixir’s core concurrency primitives: spawn/1, send/2, recei...
New
zorn
A recent ex_money v6 upgrade was blocked because Timex pins an old gettext. Rather than one big remove-and-rewrite PR, I used a shim: a m...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement