What is the starting list_of_assigns value?

I was curious what the list_of_assigns starting value is so I created a preload callback in the default LiveComponent that gets created when you use the generator to create a new LiveView (e.g., ExampleAppWeb.PostLive.FormComponent).

My understanding is that it is a list of maps.

I wasn’t successful in capturing the value for some reason. I stop the server and restarted it. Is there something else happening that prevents the preload callback to run the first time the form loads, like client-side caching.

Anyways if anyone knows what the starting list_of_assigns value is, I’d like to know. Thanks!

1 Like

Going off of the docs and typespecs for preload/1, “it is invoked with a list of assigns for all components of the same type.”

@callback preload(list_of_assigns :: [Phoenix.LiveView.Socket.assigns()]) ::
  list_of_assigns :: [Phoenix.LiveView.Socket.assigns()]

If I recall correctly, the LiveView templates that are generated by default don’t include multiple form LiveComponent of the same type. It might be worth trying to render more than one and see if that makes a difference.

<.live_component :for={id <- @ids} id={id} module={ExampleAppWeb.PostLive.FormComponent} />

Also potentially of interest for you is the preload logic in the module Phoenix.LiveView.Diff:

2 Likes

What do you mean by “mutiple form LiveComponent”? Here’s what I was going off of: Phoenix.LiveComponent — Phoenix LiveView v0.18.16

<.live_component module={A} user_id="1" />
<.live_component module={A} user_id="2" />

If that’s in the parent than A.preload would get a list of [%{user_id: 1}, %{user_id: 2}], so you can load both users with a single query.

2 Likes

Ok that makes sense. It would also explain why preload wasn’t working because I wasn’t actually using it in a template anywhere.
Thanks!