Elixir_begginer

Elixir_begginer

Preserving UI state in Live Layout

Hello,

I’m a total Elixir and Phoenix beginner, coming from js and react world.

I was wondering if anyone could help me solve an issue I can’t get my head around.

I’m building an admin dashboard type of project and I have the same layout (navbar and sidebar) for lots of live pages. The admin dashboard will have “Toast notifications”. So what I did is, I’ve wrote some js code to generate the toast, added it to the js hooks, and I’m triggering the toast with a “push_event” (“toast”) on a click of a button.
The toasts then appear in the root container div, that I’ve put in the “admin” layout.

The toast root lives here in the “admin_layout”:

<main class="mx-auto px-2 pt-11 lg:px-6 2xl:ml-56 overflow-auto max-h-screen">
  <.flash_group flash={@flash} />
   <%= @inner_content %>
<div id="notifications-root" phx-hook="Notifications"></div>
</main>

What I did is put all of those pages in a live_session with the same “admin_layout”;

  scope "/", Web do
    pipe_through([:browser, :require_authenticated_user])
    live_session :require_authenticated_user,
      layout: {Web.Layouts, :admin_layout},
      on_mount: [{Web.UserAuth, :ensure_authenticated}] do
      live("/users/settings", UserSettingsLive, :edit)
      live("/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email)
      live("/office", OfficeLive)
    end
  end

So when I press the button and a toast appears, everything works correctly. However when I live navigate to a different admin view, for example from “summary” to “users”, the toasts dissapear and the console shows an error:

phx-F4H9jaHF_fjKQwji destroyed: the child has been removed from the parent - undefined

I don’t understand why that happens, as the root div continues living in the layout when I change the page (the layout is unchanged), but the toasts living in the root div get removed.

Is there a way to make this work correctly? Thank you for your time

First Post!

sodapopcan

sodapopcan

Hello and welcome to the forums!

LiveView doesn’t track changes, or even know about, the root layout. Moving your toast code to app.html.heex layout will fix this.

This section of the docs, particularly the last paragraph, will give more insight as to why!

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It is indeed funny how the scope creeps. Originally the goal was explicitly not to take on SPAs, the goal was just to enable some dynamic content on a page without resorting to JS.

Definitely over time that line has gotten blurrier.

Elixir_begginer

Elixir_begginer

Dont worry about the mixup :slight_smile:

No, I didnt touch the flash_group or flash components at all.

I will try more hacky solutions for now. Thank you for your time.

Last Post!

sodapopcan

sodapopcan

Yep, one process == LiveView. There are no other user-dedicated processes started by the framework, it’s up to you to start more, which I think is a fair design. I also appreciate being able to implicitly throw away state by switching to another LiveView. If you want to be more SPA-like you can make use of many LiveComponents inside a single LiveView. I’ve never gone too far down that route myself so I’m not sure how hairy it would get.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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 42716 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement