PJUllrich
Pass unspecified assigns Map to LiveComponent with LiveView 0.17.2
I updated a project to the latest Phoenix LiveView 0.17.2 and can’t figure out how to pass assigns to a LiveComponent. I guess I need some kind of “spread operator” here, but don’t know the proper syntax.
Previous code:
<%= live_component @socket, Web.MyModule, Map.merge(assigns, %{id: "my-component-id"}) %>
My attempts:
# Sets an `assigns` assign on the `assigns` of the LiveComponent (got it? :D)
# So, in the LiveComponent I have now `assigns.assigns.my_var` instead of
# having @my_var or assigns.my_var
<.live_component module={Web.AddressChangeLive.ChangeComponent}, id="change-component", assigns />
# In this case, `module` and `id` are `nil`
<.live_component Map.merge(assigns, %{module={Web.AddressChangeLive.ChangeComponent}, id="change-component"}) />
Could you hep me further, please?
Marked As Solved
moogle19
You have to remove the , in your <.live_component /> tag (just like in normal html) and you can give the assigns as last argument, e.g.:
<.live_component module={Web.AddressChangeLive.ChangeComponent} id="change-component" {assigns} />
Edit: Take a look at Cost of stateful components in the documentation
Also Liked
seb3s
Hi,
you should try something like :
<.live_component module={Web.MyModule} id="your_id" {assigns} />
But generally, you should pass to your components only the data it uses (not all the assigns map)
Also note that the commas are removed (normal HTML attributes)
Cheers,
Sébastien.
moogle19
Then I won’t edit my post to add a link to the documentation next time. ![]()
PJUllrich
Ah yes! The comma was the problem! I’d like to accept both answers but since I can select only one I will choose the earliest
Thank you both though!







