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!

Where Next?

Popular in Guides/Tuts Top

redfloyd
Greetings fellow alchemists ! I have started to write an open-source interpreter in Elixir (GitHub - nicolasdilley/dwarf-interpreter: Th...
New
hlx
Typed my very first blog post ever, hope it will help some people https://henricus.xyz/roll-your-own-email-password-authentication-with-...
New
annad
I’m posting this for developers who are totally new to Phoenix like myself. This is all probably obvious to more skilled Phoenix develope...
New
zachallaun
Hey friends, wanted to share a tiny shell script I’ve been using to start Livebook with easy access to either a running production server...
New
alejandroErik
POST IN CONSTRUCTION Process for compile erlang otp 20 with odbc-unix for Oracle connections on Solaris 11.3 for 64 bits: Introductio...
New
niku
I write an article Parameterized testing with ExUnit.The key concept is using ExUnit.Case.register_test/4 such as ExUnit.start() defmod...
New
tonydang
I recently got inertia-phoenix (an Inertia.js adapter for Phoenix) working with Svelte. Setting up the server-side rendering (SSR) with S...
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
dmitriid
This is not a question, but a post for anyone who may need it in the future. Sometimes some browsers may mangle the filename if it’s non-...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
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
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement