Liveview update only changed assigns

hi,

I have a form which would update the assign form_data in socket. I also have a tags assign which contains a list of tag names, a focused_tags assign which contains tag names that have been clicked.

Now, when I click a tag, I update the focused_tags accordingly. Problem is, whatever new values in the form will be reseted every time I update focused_tags (and only update it). Basically I did:

...phx-click="tag_clicked" phx-value-tag_id="<%=tag.id%>"...
{:noreply, sock |> assign(:focused_tags, [new_tag | focused_tags])}

Is there a way to have Liveview render only the relevant part (which is highlighting clicked tag names)? I thought that is the goal of Liveview, or did I do something wrong?

Thanks a lot…

Live view is built on the idea that the HTML view is deterministically derived from the assigns. That is, a given set of assigns should always result in exactly the same HTML every time. If you want the form to not be clobbered, you need to tell the live view about changes to the form via phx-validate so that it can track those changes.

LiveView does perform optimizations to minimize diffs sent, but that should be considered an implementation detail, not relied upon for having changes work one way or another functionally.

3 Likes

thanks a lot, that clears up my doubt! I will check the phx-validate thing…

1 Like