Phoenix.LiveView.send_update
send_update
, go back to Phoenix.LiveView module for more information.
Asynchronously updates a Phoenix.LiveComponent
with new assigns.
The component that is updated must be stateful (the :id
in the assigns must
match the :id
associated with the component) and the component must be
mounted within the current LiveView.
If this call is executed from a process which is not a LiveView
nor a LiveComponent, the pid
argument has to be specified.
When the component receives the update, first the optional
preload/1
then
update/2
is invoked with the new assigns.
If update/2
is not defined
all assigns are simply merged into the socket.
While a component may always be updated from the parent by updating some
parent assigns which will re-render the child, thus invoking
update/2
on the child component,
send_update/3
is useful for updating a component that entirely manages its
own state, as well as messaging between components mounted in the same
LiveView.
Note: send_update/3
cannot update a LiveComponent that is mounted in a
different LiveView. To update a component in a different LiveView you must
send a message to the LiveView process that the LiveComponent is mounted
within (often via Phoenix.PubSub
).
Examples
def handle_event("cancel-order", _, socket) do
...
send_update(Cart, id: "cart", status: "cancelled")
{:noreply, socket}
end
def handle_event("cancel-order-asynchronously", _, socket) do
...
pid = self()
Task.async(fn ->
# Do domething asynchronously
send_update(pid, Cart, id: "cart", status: "cancelled")
end)
{:noreply, socket}
end