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?