elt547

elt547

All logic being push from LiveComponents to LiveView processes because of lack of assigns sharing? What's the best way to change?

I often find that I have trouble “reacting to events” in my LiveComponents. The reason for this is that the LiveComponent assigns are separate from the LiveView assigns. If I wanted to get everything done inside the component, I would have to pass EVERYTHING, even the stuff that exists everywhere such as current_account.

The way that I’ve been handling this is using Kernel.send/2 to send messages to the parent liveview process. It feels really wrong though, I’m ending up with a lot of almost empty components that look like this:

  def handle_event("request_deletion", _params, %{assigns: %{post: item}} = socket) do
    send(self(), {:request_deletion, item})
    {:noreply, socket}
  end

  def handle_event("request_share", _params, %{assigns: %{post: item}} = socket) do
    send(self(), {:request_share, item})
    {:noreply, socket}
  end

  def handle_event("request_follow", _params, %{assigns: %{post: item}} = socket) do
    send(self(), {:request_follow, item})
    {:noreply, socket}
  end

This is pushing all of the logic to my LiveView modules and I can see things getting quite heavy in my liveviews. It feels very odd to be writing a bunch of logic in LiveViews when that logic belongs to the LiveComponents themselves. At the least, is there a way that I can automatically pass the current_account and other “everywhere assigns” through from the LiveView to my LiveComponent instances?

First Post!

cmo

cmo

Stuff does not exist everywhere. It is important to understand that there is no global state. Stuff (assigns) is not magically passed down to components. The framework is not capable of determining which assigns you want to pass to each component automatically. This is your responsibility.

If your events are destined for the LiveView, why not send them straight there? There is no need to target the component.

One thing you can do is create on_mount hooks for your components and mount them in the LiveViews that use that component. Make sure you namespace your events.

You can also ask yourself, does this really need to be a livecomponent or is it really a function component I’m dressing up as a livecomponent?

What is the state you’re maintaining in the component that you need to send every event to the LiveView?

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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

We're in Beta

About us Mission Statement