Reasoning about assigns in Liveview

Hello,

I want to make sure my reasoning about assigns in Liveview is correct.

My understanding of assign/3 is as follows:

-If I assign a previously non-existent assign, it will be added to the list of existing assigns
-If I assign an assign that already exsists, it will be updated/replaced
-Any other already existing assigns that are not mentioned in my assign/3 call will remain unaffected

Would this match your understanding also?

Thank you!

1 Like

Yes that is correct.

Also, something I’ve learned yesterday, which might be obvious: the update/2 callback on LiveComponent is called only when the parent updates any assign on the component, or calls send_update on it. It will not be called during assign/3.

Example:

## In Parent

# Component.update/2 called whenever `foo` changes
<%= live_component @socket, Component, id: "component", foo: @foo %>

# Component.update/2 called here as well
send_update(Component, id: "component", foo: :bar)

## In Component

# Component.update/2 not called here
assign(socket, :foo, :baz)
1 Like

Great, thank you @1player :slight_smile:

Very good to know about the update/2 on LiveComponent!