Hi!
I’m wondering what is the best/most idiomatic way to communicate between a LiveComponent and its parent, also a LiveComponent.
I have LiveComponent A
which renders LiveComponent B
. Once I interact with B
, it will eventually need to update A
. A
will also update the parent live view, but this is not an issue right now.
Some possible solutions would be:
Keep the LiveView as source of truth
I could keep all the state for A
in the LiveView and pass it down, but it kinda defeats the purpose of having a LiveComponent to put the state in it.
Communicate through the LiveView
I could keep the state in A
, and use the LiveView as a sort of message broker between B
and A
: B
can use send/2
and update its grandparent LiveView, then inside handle_info/2
the LiveView can update A
.
I don’t like this solution because then the LiveView needs to know about B, and would scale poorly
Use send_update
B
can use send_update/2
targeting A
and its id. This looks great BUT it requires B
to know its parent, which means I can’t readily use B
in a different context. Maybe if I passed the module and id to B
as parameters?
Am I missing some idiomatic option to communicate between LiveComponents or some important details about the solutions I have already explored?
Thanks in advance!