marcuslankenau

marcuslankenau

Communication between live components?

Hi,

I evalute liveview and cosider moving our current react solution to liveview at some point.

For that I try to rewrite one of our pages. It is a list page, listing the main entity of our app. It also allows to select entities and invoke actions. Each action is shown as a dialog.

That woks well so far, but now I am running into a problem:

For one specific action, filtering for customers, I show a dialog. The dialog displays two select widget, selecting customers to include and to exclude. The select widget is rather complex and allows to select a set of customers.

The dialog and the contained select-widgets are both live components. In the select widget I want to notify the parent (dialog) about changes of the selection. But I see no valid way of doing that.

  • By Sending a message, I will only reach the liveview.
  • Using the example code in the phoenix docs, sending a function as parameter to the select widget is also difficult, because I do not want to send the results right away to live view, I rather want to update the state of the dialog

My prefered solution would be to trigger an event (like phx-change) in the select widget that would be received by the dialog. Is that possible?

I hope the question isn’t too confusiong.

Most Liked

josevalim

josevalim

Creator of Elixir

EDIT: this is my opinion and not an overall position of the Phoenix team about LiveComponents. Give it a try and evaluate it for yourself.

IMO LiveComponents are one of the most overused features of LiveView. It makes sense from a historical sense, they were one of the first abstractions to be added, but today we have function components, LV hooks, JS commands, and others which can be better suited for creating abstractions.

For example, before Phoenix v1.7, everyone used LiveComponents to create modals, and then the Phoenix team showed it could be done with plain function components plus JS commands. Given function components are the simplest abstraction, then they should be the first choice.

If they are not enough, I’d say hooks are more powerful and provide a better foundation than LiveComponents for building new abstractions. For example, imagine you are creating a “live table” with pagination and search. Most people would immediately design it as a LiveComponent for reuse, but I would rather introduce a LiveTable data structure and build on top of that. Something like this:

assign_live_table(socket, :posts, arg1, some_opt: ...)

And now, all of the state of the table is stored directly in the socket, which you can access and manipulate through additional functions. When it comes to rendering, you pass the table plus the name of any event you want dispatched, as if it was any other function component/html element:

<LiveTable.render table={@posts} row-click="some-event" />

Inside LiveTable.render, you can use JS commands to dispatch events and, if there are internal events, you can use attach_hook during “assign_live_table” to consume them.

This is precisely how streams and assign async are designed and implemented in LiveView itself.

We have tried this direction with couple of our Dashbit clients and, every time so far, it has led to better designs: components that are easier to use and tests. Of course, live components still have their use cases, especially when breaking down a LiveView into non-reusable parts, optimizing what goes over the wire, etc. I don’t know what is the best answer here, as we don’t have enough context, but I thought I would bring this other option to the table.

krasenyp

krasenyp

In my previous life, as a front-end focused developer, we used to say “data down, events up”. I refer to it when evaluating component designs. In my opinion, live views should orchestrate communication between live components. The latter send events to the parent live view, it does what it does, and updates it’s assigns, which are also the inputs of the live components. Communication via send_event between components that are on the same level in the hierarchy is a code smell.

Zurga

Zurga

How would you handle multiple LiveTable components in one LiveView?

Last Post!

Chrichton

Chrichton

Hi all

I must admit, I use phx.gen.gen.live to generate simple input forms for data-access.
This command generates a LiveComponent.

Am I right, that this isn’t a good idea?
Should that change in the future?

Cheers, Heiko

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement