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

cheerfulstoic
I spent quite a bit of time trying to find a good configuration to build / test my Elixir app in CircleCI and then push an image to Docke...
New
eclark
I’ve been working on a phoenix project lately and I wanted to use the latest versions of everything. Webpack 5 had some breaking changes ...
New
hauks96
Hello everyone, I created a deployment tutorial for Phoenix applications with Kubernetes (microk8s) a few months back with the goal of s...
New
egze
I was preparing to deploy a production application to AWS Fargate, and to practice I wanted to play with DNS polling and node discovery o...
New
Neurofunk
Hi folks! I wrote an article about BEAM and Haskell Interoperabilty. I'm describing the use of the erlang-ffi project, that impersonates ...
New
voltone
The EEF’s Security WG has released the first public draft of the Secure Coding and Deployment Hardening Guidelines for BEAM languages. “...
New
nelsonic
Complete beginners Todo List Tutorial in Phoenix 1.5.3 (latest and greatest): It’s a feature complete TodoMVC clone with 0% JavaScri...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
benwilson512
Correct if me I’m wrong, as best I can tell there aren’t any reasons to use mix run --no-halt in production vs releases. The marginal val...
New
emilsoman
Hello, While answering a StackOverflow question on how to debug an elixir node running remotely, I thought it may be helpful to write a...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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