Elixir_begginer
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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.heexlayout will fix this.This section of the docs, particularly the last paragraph, will give more insight as to why!
Elixir_begginer
Hello, thank you very much for your reply.
However, I should say that the layout I posted (
admin_layout) is not theroot_layout.The problem exists in the
admin_layout(might as well be theapp.html.heex). When I put thetoast root divin theroot_layout, everything works fine (because then thistoast root divis not touched by the live navigation and sockets mounting / unmounting). So if thetoast root divis in theroot_layout, the toast remain on screen after live navigating, which is the behavior I want.However I would like to have this same behaviour when putting the
toast root divinside theadmin_layout(which is the equivalent of theapp.html.heexyou’ve mentioned). The problem is that if I do it like that, all the children of thetoast root div(the toasts) get removed, as if the whole layout has re-rendered.I also tested your recommendation, and I’ve put the div inside the
app.html.heexlayout, however the problem persists.sodapopcan
Oh, yikes, my bad! I read “root” and assumed you meant meant it was a root layout though from your code it’s obvious
You’ve also thrown how I think about the root layout for a loop, lol. Sorry about that!
Have you modified
flashorflash_groupcomponents at all? If so could you share them? Otherwise I’ll have to let someone else chime in as I don’t see anything wrong with your code. I took a look at a few of my projects and they all look like yours with custom admin layouts and all.kokolegorille
You could use a nested liveview, in charge of all notifications.
BTW, there is root, app layouts, and also live layout
Elixir_begginer
Dont worry about the mixup
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.
Elixir_begginer
Thank you for your reply. From a short google search, nested liveviews seem to be exactly the solution. However, I was unable to find any docs of how to implement them. Can you point me to where i could find some instructions?
Also, can you maybe make a short explanation of what is the difference between root, app and live layout? I know its a hassle so if you can only address the question about nested liveviews that would be great as well
kokolegorille
Root is shared between both normal and live views. It is also the start of the layout.
App is for normal views
Live is for live views
I use something like this in the live layout. I had to activate it, because it’s not there by default I think.
I use this because I want to delegate notifications to the nested liveview.
Also…
root and app use @conn, while live use @socket
sodapopcan
Take a look in your
lib/my_app_web.ex, specifically at the:controllerandlive_viewhelpers. If you wanted your live views to uselive.html.heexfor live views, then you would changto
and create
live.html.heexinlib/my_app_web/components/layouts/.The root layout is specified in the router (look for
put_root_layout).Really the docs I link do a good job of explaining the difference between root and app layouts (
liveis considered an “app” layout), especially the last paragraph:Elixir_begginer
Thank you for the reply. I should say that I found out that what im trying to do is not possible (at least I think) with the built in live layouts.
As far as I was able to deduce, the layout always uses the same process of the current liveview. The problem arrives when live navigating from one liveview to the other, as this starts a new liveview process. Because the built in layout is always the part of the current process, it loses the state when navigating between liveviews.
So as far as I can tell the only saving grace is to have the layout (navbar and sidebar) be a child liveview process with the
stickyoption set to true, so it lives in a world of it’s own.This is a very strange design (?) I might say. I would have thought, that giving that liveview is kind of aimed at providing a SPA experience, this basic feature would have been worked out better.
benwilson512
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.