kuon

kuon

Emulating "send()" and "handle_info" with live components

I have a page with a large state that is loaded from DB, let’s call that “data”.

I have a root live view that load “data” on mount and render different live components (one component per tab).

This is to allow switching tab without hitting the DB.

Now, my issue was that each component is really isolated for the state (like selection), and I wanted to be able to do something like send(self(), {:select_part, part}) and do the handle_info in the component rendering the current tab, and now the “root live view”.

I found a neat solution I want to share as it might be a common issue.

In the deep down component, instead of doing send() do send_update(DrawTabComponent, id: "draw_tab", action: {:select_part, part}).

Then in the DrawTabComponent implement update like:

  @impl true
  def update(%{action: {:select_part, part}}, socket) do
    {:ok, assign(socket, selected_part: part)}
  end

  @impl true
  def update(regular_assigns, socket) do
    {:ok, assign(socket, regular_assigns)}
  end

This allow for the same organization/code split as nested live view. Each update(%{action implementation is similar to a handle_info, without overriding the regular update, thus allowing a similar decoupling as live view mount/handle_params and handle_info

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix

send_update is for exactly this kind of thing :slight_smile: I also do the :action key to handle non-render updates, so looks great!

Last Post!

jcorte-real

jcorte-real

Thank you so much, this helped me a lot!

Where Next?

Popular in Guides/Tuts Top

zenw0lf
Hello all! For those wanting to try your hands at Elixir / Phoenix, I wrote a comprehensive tutorial on doing a simple JSON API with sai...
New
marcelo
I wrote a small article on how to use current_user with coherence on Phoenix. There are already a couple of blogposts about it but I thin...
New
anuragg
We finally have a Mix clustering guide to go with Phoenix deployment with Mix Releases. Deploy an Elixir Cluster with Mix Releases and l...
New
nelsonic
When we were figuring out how to use Phoenix LiveView we got stuck a few times. So in order to save other people time, we created a comp...
New
berts-4865
Here is a quick guide to uploading a file from the browser to DO spaces. It is crude, but will hopefully save sometime time and frustrat...
New
water
I’ll post this here, It might help someone in the future. Feedback is greatly appreciated. I use it with direnv on NixOS, It should wor...
New
georgeguimaraes
Another cool plugin for Neovim, GitHub - jmbuhr/otter.nvim: Just ask an otter! 🦦 · GitHub makes it possible to run linters for embedded c...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement