Live layout assigns available but not @socket.assigns

Hello,

when using a live route with url put in assigns by handle_param callback like so

def handle_params(params, url, socket) do
socket = assign(socket, :url, URI.parse(url))
{:noreply, socket}

end

inside the live layout @url is available but surprisingly @socket.assigns is consistently #Phoenix.LiveView.Socket.AssignsNotInSocket<>

what is the explanation of this counter intuitive situation ?

Thanks

Best regards

Not using @name to fetch data breaks change tracking of liveview, so @socket.name is made to raise an error.

1 Like

You get that "error’ because that is not the correct way to fetch assigns (as mentioned by @LostKobrakai). Previously there were docs for that module that clarified some more (but it looks like they’ve been removed after 0.15.1): Phoenix.LiveView.Socket.AssignsNotInSocket — Phoenix LiveView v0.15.0?

The socket assigns are available directly inside the template as LiveEEx assigns, such as @foo and @bar. Any assign access should be done using the assigns in the template where proper change tracking takes place.

2 Likes

ok it’s related to change tracking and it is kind of a warning. But AssignsNotInSocket is a paradoxal message, since there are assigns in the socket

also @socket.name is made to raise an error, but is it the case for @socket.assigns.name ?

also inside the live module different handlers you can use socket.assigns.name to get the value or pattern match

would it be any clearer change the message to something like AssignsNotAddressableThisWay in the templates ?
Thanks
Best Regards