trestrantham
I have a LiveView that renders a LiveComponent via an leex template:
defmodule MyAppWeb.ThingEditView do
use Phoenix.LiveView
def mount(_params, _session, socket) do
{:ok, socket}
end
def render(assigns) do
MyLiveView.render("edit.html", assigns)
end
def handle_info({:update_thing, thing, thing_params}, socket) do
case Thing.update_thing(thing, thing_params) do
{:ok, thing} ->
{:noreply,
socket
|> put_flash(:info, "Thing updated successfully.")
|> push_redirect(to: Routes.live_path(socket, MyAppWeb.ThingEditView, thing))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end
The template renders the LiveComponent like:
<%= live_component(@socket, MyAppWeb.MyLiveComponent) %>
When ThingEditView is rendered after the push_redirect above, I can see the flash message in MyAppWeb.ThingEditView’s socket.assigns.flash. I would then expect by passing socket into MyLiveComponent I would be able to pull out the flash message in the LiveComponent via live_flash(@flash, :info).
However, socket.assigns.flash is always an empty map when my LiveComponent is rendered. I never call live_flash/2 in the LiveView and would expect the flash message to remain in the socket assigns until I pull it out in my LiveComponent.
Is this the correct way to be thinking about flash with LiveView? Am I missing something that would enable to behavior I’m after?
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
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sfusato
My understanding is that the combo
put_flash+plug :fetch_live_flashis a convenience that will make the flash messages available in your main live view under the assignflash. In order to have it available in other liveviews/components rendered in your main liveview, you would have to pass it further along as an assign.trestrantham
Explicitly passing @flash down through the child components does get the behavior I’m after. However, there are many layers of nested LiveComponents in my actual app and I was hoping to avoid passing flash through all of them and instead pluck it out of the socket in the specific child component that needs it.
chrismccord
Passing flash down as assigns is the way to go. Note, that you may want to pass it down as
@parent_flashor similar as the nested components may have their own flash to display which you don’t necessary want to display on the parent.trestrantham
Thanks Chris!
I was thinking about flash in more request/response lifecycle. But it sounds like every LiveView/LiveComponent will have its own flash, which would make sense. Just so I’m clear, does this mean that
put_flash/2will only set@flashfor the LiveView it’s called from and not the socket in general?chrismccord
This is correct. The only nuance is put_flash + redirect from a nested LV or component will be picked up by the root LV.
ndrean
Stumbled on this too. In my case of a nested child component but with no redirection, an easy option is to
senda message from the child component so the parent LV parent canput_flashin thehandle_info.jtormey
This approach can also be abstracted out so that it doesn’t have to be repeated in every LiveView, using a helper module with
on_mount/4andattach_hook/4.I’ve done this in the past:
Then, in
my_app_web.ex:Now calling
push_flash/2from a LiveComponent will make the parent LiveView render the flashKristerV
very cool. i’m using phoenix 1.7 RC2+ (actually git master due to bugs) so had to make a few adjustments.
live_flash.exapp_web.exvery clever, thanks @jtormey
annad
Thank you @KristerV and @jtormey for this code! I was struggling to get my Live Component to flash when there was an error. I set up this LiveFlash and it works great – except for one small problem. I can’t get the flash message to go away.
I tried using
clear_flash(socketin my component when the user enters new information but that didn’t work. I also tried adding<div phx-window-keydown="clear-flash"></div>in the LiveView but that didn’t work either. I then triedphx-click="lv:clear-flashin the component form which has a phx_target={@myself} but that also did not work.The following code is already in my live.html.heex so it seems like it should be clearing the flash message when there is a key click:
I’m running out of things to try.
Could you tell me what code you use to clear the flash?
UPDATE: I think I figured it out. There is a
handle_eventin the LiveComponent for handling the text_input. In that handle_event, I addedpush_flash(:error, nil)and that clears any previous flash messages. Not sure if that is correct, but it works!Odd that the code above in my live.html.heex file doesn’t clear the flash message when the user clicks onto the page again. I’m assuming it doesn’t work because the form text_input is in a LiveComponent so the phx_click isn’t registering.