My component:
defmodule BwWeb.CartComponent do
use BwWeb, :live_component
def mount(socket) do
IO.puts("CartComponent mount")
{:ok, assign(socket, show: false)}
end
def update(
%{
id: _id,
cart_products: cart_products,
cart_id: cart_id,
store_id: store_id,
store_slug: store_slug,
show_checkout_button: show_checkout_button,
} = _assigns, socket) do
{:ok, assign(
socket,
cart_products: cart_products,
cart_id: cart_id,
store_id: store_id,
store_slug: store_slug,
show_checkout_button: show_checkout_button
)}
end
The component is called in the live view template:
<%= live_component(
@socket,
BwWeb.CartComponent,
id: Ecto.UUID.generate(),
cart_products: @cart_products,
cart_id: @cart_id,
store_id: @store.id,
store_slug: @store.slug,
show_checkout_button: true
) %>
In the live view I handle a pubsub notification:
def handle_info({Bw.Man.Cart, [cart_id, _], _}, socket) do
IO.puts("handle_info")
cart_products = Bw.Man.Cart.get_cart_products(cart_id, socket.assigns.store.id)
{:noreply, assign(socket, cart_products: cart_products)}
end
Always when this handle_info is executed, the CartComponent is mounted.






















