LiveComponent handles first event, doesn't change assigns on second

I have this live component that I render like this

<.live_component
  module={PdexWeb.SearchComponent}
  id="search-results"
  show={true}
/>

This is its definition

defmodule PdexWeb.SearchComponent do
  use PdexWeb, :live_component

  @impl true
  def mount(socket) do
    {:ok, socket, temporary_assigns: [entities: [], state: "closed", count: 0]}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div>
        <button phx-click="increment" phx-target={@myself}>click me! <%= @count %></button>
    </div>
    """
  end

  def handle_event("increment", _, socket) do
    dbg(socket.assigns.count)
    new = socket.assigns.count + 1
    {:noreply,
      socket
      |> assign(:count, new)
    }
  end
end

when i click the button in the browser, the value increments once, but on subsequent calls, the value of :count is 0

What am I doing wrong ? Any help would be much appreciated :slight_smile:

Remove count from temporary_assigns, set it as a regular one.

1 Like