petelacey

petelacey

LiveView form recovery does not work if components are removed from and added to the DOM

I’ve got an interesting one for all you LiveView peeps out there. I’m not 100% sure of my analysis, but allow me to paint a picture before I get to my question.

The system I am building is essentially a workflow-specific Kanban board with task cards arranged in a grid.

The kanban board is a LiveView. Clicking a card uses live_patch to open it in a modal dialog as a LiveComponent. On the card is a form with a phx-change event used for both validation and auto-recovery purposes. This question is going to be about auto-recover.

On page load of the LiveView (Kanban board), Phoenix gives the LV an ID, e.g. phx-Fn1s7NJqKqkaDAGB. When the first LiveComponent (the modal) is rendered, it gets an id too: phx-Fn1s7NJqKqkaDAGB-1-0, and so does the nested LiveComponent (the card): phx-Fn1s7NJqKqkaDAGB-2-0. As you can see, each LiveComponent gets the parent LV ID and an incremented suffix.

To test data recovery, I have exposed the LiveView socket on the Window object as ls, and can use ls.disconnect() and ls.connect() in the browser’s console to simulate a network drop. When I enter form data in the nested component, disconnect, and reconnect, the form data is recovered and everything’s grand. This works, because the newly generated live components have the same IDs as their already existing client side counterparts, i.e. phx-Fn1s7NJqKqkaDAGB-1-0 & phx-Fn1s7NJqKqkaDAGB-2-0.

However, when I close the modal normally (live_patch back to the LiveView URL), destroying the live components, and click on another card (or the same card, doesn’t matter), the live components get new IDs; the modal gets phx-Fn1s7NJqKqkaDAGB-3.0 and the card gets phx-Fn1s7NJqKqkaDAGB-4.0. Repeating this generates IDs 5 and 6, then 7 and 8, and so on.

No problem really, except that now when I drop and restore the network, after the LiveView gets re-rendered all unsaved data is lost. This is because the component ID suffixes are regenerated from 1 after the mount (as before), but their client side counterparts are not suffixed 1-0 and 2-0 as they were the first time, they are instead numbered, for instance, 7-0 and 8-0. Consequently, these client-side components from before the disconnect get destroyed and auto-recovery is not attempted.

To restate, if a LV component is removed from the DOM and added again, form auto-recovery will not work. Or so it appears.

Now, I don’t think anyone—even me—would call this a bug, but I was wondering if anybody had any thoughts on this behavior or, ideally, any workarounds. I suppose I could write an Amnesia-backed phx-auto-recover event or some such, but that seems like overkill for a simple form. I’m willing to do it, but I am hoping for some additional insight first. Maybe a client-side solution would be better? Maybe my analysis is all wrong? Any thoughts?

Marked As Solved

petelacey

petelacey

FWIW, I was able to work around this issue with a judicious sprinkling of phx-update="ignore" attributes on the relevant forms. I’m not sure I’m crazy about this solution, but then I’m not sure why I’m not sure either.

Also Liked

derek-zhou

derek-zhou

I only use state-less components so I don’t have this problem. I guess my conservative attitude towards fancier tech pays off sometimes.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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 31265 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement