defmodule Test.ThermostatLive do # In Phoenix v1.6+ apps, the line is typically: use MyAppWeb, :live_view use Phoenix.LiveView # use Phoenix.LiveComponent def render(assigns) do ~H""" <%= for {id, card} <- @cards do %> <.live_component module={CardComponent} id={id} card={card} /> <% end %>
""" end def mount(_params, _session, socket) do IO.inspect("mount") cards = %{"first" => %{title: "foo"}, "second" => %{title: "bar"}} {:ok, assign(socket, :cards, cards)} end def handle_event("click", _, socket) do IO.puts("click root") # update all the cards cards = socket.assigns.cards |> Enum.map(fn {id, card} -> {id, %{card | title: card.title <> "."}} end) {:noreply, assign(socket, :cards, cards)} end end defmodule CardComponent do use Phoenix.LiveComponent def render(assigns) do ~H"""
""" end def handle_event("click", _, socket) do IO.puts("click component") # socket.assign() old_card = socket.assigns.card new_card = %{old_card | title: old_card.title <> "."} {:noreply, assign(socket, :card, new_card)} end end