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}

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

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

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?

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?