Gilou06

Gilou06

Hello,
I wonder if is is feasible to render the same LiveComponent instance in two locations within a LiveView.
The typical case would be a LiveComponent in charge of a state, I would need to display and modify some fields in one HTML location of the LiveView, and the other fields somewhere else (HTML location).
Or, in other words: LiveComponent manages state data= %{a: one, b: two, c: :three}, I need to have a form about a at the top of the LiveView HTML page, a form about b and c at the bottom of the LiveView HTML page.
Of course, the LiveComponent would process the DB access for data as a whole.
That would require calling the LiveComponent twice within the LV, with same id and a way to select a different render function.
Is this crazy thinking ?
Thank you

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

You cannot have the same LC using the same id multiple times. The whole reason there’s an id is that it uniquely identifies the LC. The also cannot share their state. But it sounds like what you’re looking for could be done without those two things anyways. Have a LC to handle the form – pass in a, b, c individually.

Gilou06

Gilou06 OP

Hi,
Ok, but I actually need two forms, one for a, one for b&c since those two forms are going to be displayed in separate location of the rendered calling LiveView.
So two forms coded in the same LC but displayed in separate LV locations. Seems impossible.

LostKobrakai

LostKobrakai

Why have the two separate forms be handled by one LC though?

Gilou06

Gilou06 OP

I wanted to regroup within the same LiveComponent forms and database code related to the same data.
That way, the forms & its events are processed within the same LC.
How would you do it : define the forms in the parent LV and send the events to the LC state manager ?

LostKobrakai

LostKobrakai

The parent LV (or LC) is the only thing containing all parts. If you really want one piece handling everything it should be that parent. Having a single state handling component (process for LV or LC) handle multiple distinct parts of the markup would make things confusing at best imo.

Gilou06

Gilou06 OP

I feel you.
Initially everything was located in the same form and I was happy like a puppy. Then for UI reasons I had to move one of the fields away (HTML wise) from his brothers. And what appeared like a simple graphic change lead to a (possibly) massive change in the code.
My thinking : it would be great to have local state management (LC takes care of changesets, DB…) but spread UI rendering : where things are in the HTML finale page.
But this is the web, not a local app…

LostKobrakai

LostKobrakai

You’re free to move business logic to a shared/common place outside of LV/LC implementations. But the LV/LC modules themselves are tightly linked to markup and its hierarchical tree structure.

Gilou06

Gilou06 OP

I’m not arguing here but I’m rather in a learner position.
My business logic is already in a dedicated module.
Then let me rephrase with what I learned from you. : What I’m trying to do is to have the state common to two LiveComponents.
That would require some communication between the module in charge of the common state (a genserver) and the LCs in charge of the markup.

  • Form in charge of “a” located in LC “A”
  • Form in charge of “b” and “c” located in LC “BC”
  • common state for %{a,b,c} in a dedicated Elixir genserver. Genserver performs send_updates to “A” and “BC”
  • “A” and “BC” call genserver API when they need to update state
    Or some pubsub instead of send_update
LostKobrakai

LostKobrakai

To me it’s still a bit unclear though why you need a, b, c to be handled together. I generally consider forms to be “low level crud’ish operations”, which might or might not line up with higher level business logic. If they don’t line up you likely want to handle the low level stuff separately from the high level stuff.

Gilou06

Gilou06 OP

I’ll review the code in light of your statement.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews