sjolicoeur

sjolicoeur

Rendering liveview inside another liveview with live_render generates a not_mounted_at_router error

Hello,

full disclosure I am still very new to elixir and phoenix and liveview.

I am trying to transform a liveview controller and it’s. *.leex template into a component. The controller was auto generated with mix phx.gen.live and I am trying to turn the :show controller into a re-usable component that can be embedded in other views.

The first of which was to insert it into page_live.html.leex which according to the docs should work when

<%# live_render(@socket, ChatDemo.ChatRoomLive.Show, id: "ChatWindow", session: %{"id" => "07593216-b033-40e1-8c03-f32aa7375dec"} ) %>

But I get a no match of right hand side value: :not_mounted_at_router and somehow the debug page doesn’t show the expected content for session. I know it does require an :id in the params.

the controller works as a standalone, but not when rendered in another *.leex, is it possible to write a mount function that will work with live_render ? What am I doing wrong? or miss understanding?

the router entry:

  scope "/", DemoWeb do
    pipe_through [:browser, :admin_view]
    live "/message/:id", MessageLive.Show, :show
    live "/message/:id/show/edit", MessageLive.Show, :edit
  end

the liveview render call

<%= live_render(@socket, DemoWeb.ChatRoomLive.Show, id: "ChatWindow", session:  %{"id" => "07593216-b033-40e1-8c03-f32aa7375dec"} ) %>

the code seems to fail here

@impl true
  def mount(params, session, socket) do
    %{"id" => id} = params # FAILS HERE, but session only contains the csrf token
    if connected?(socket), do: Chat.subscribe()
    messages = Chat.list_message_for_chat_room(id)
    {:ok, socket
          |> assign(:messages, messages)
          |> assign(:message, %DemoWeb.Chat.Message{})
    }
  end

Marked As Solved

APB9785

APB9785

Creator of ECSx

From Phoenix.LiveComponent - Targeting Component Events:

If you want to send the event to yourself, you can simply use the @myself assign, which is an internal unique reference to the component instance

Note @myself is not set for stateless components, as they cannot receive events.

So yes, @myself is specific to stateful components.

As for handle_params/3, this function is not for LiveComponents to handle events, it is for the parent LiveView to handle URL parameters. LiveComponent events are handled with handle_event/3 - either in the LiveView (if the component is stateless), or in the component itself (if the component is stateful AND the event has phx-target="<%= @myself %>")

Looking through the mount/3 function in the OP, I think there is also a lack of context here that might make it difficult to troubleshoot this issue. You’re talking about a LiveComponent, but mount/3 is the mount for LiveViews (components use mount/1). If you’re still having trouble, please share the full repository so we can see how the LC is interacting with the parent LV.

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New

Other popular topics Top

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 41454 115
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement