I think I remember reading somewhere that calling `assign/2,3` in the `render/1` callback of a LiveView or a LiveComponent is bad practice, but I don’t recall where, nor why.
Here’s an example, for sake of clarity:
def render(assigns) do
assigns = assign(assigns, :new_assign, assign.a1 + assigns.a2)
~H"""
{@new_assign}
"""
end
Is this pattern safe? If not, why not? Is this documented somewhere in LiveView’s documentation?
Thanks in advance
Where is this assign function, which takes the assigns map as a first argument, defined?
This is fine, besides the fact that it runs each time render/1 is called. So do not put expensive calculations there, but otherwise this is no different to doing the same in any downstream function components.
It’s the Phoenix.Component.assign/2 function, it works both with a socket and with a map of assigns:
The first argument is either a LiveView socket or an assigns map from function components.