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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement