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

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement