LiveForm update - after submit, I want to reset the form so they can make another submission

I have a form inside a LiveComponent. The component is a little chat widget. After submit, I want to reset the form so they can make another submission. This works the first time (the input field is cleared) — but it does not work on submission n + 1. I click submit and — despite my changeset and form data looking again clear — the form doesn’t seem to re-render.

Why?

Thank you!

defmodule ExampleWeb.ChatWidget do
  use Phoenix.LiveComponent

  alias Example.Chat.Message

  import ExampleWeb.CoreComponents

  def mount(socket) do
    form = %Message{} |> Message.changeset(%{}) |> to_form()

    {:ok, assign(socket, form: form)}
  end

  def render(assigns) do
    ~H"""
    <div>
      <.form for={@form} phx-submit="save" phx-target={@myself}>
        <.input field={@form[:body]} />
        <.button>Submit</.button>
      </.form>
    </div>
    """
  end

  def handle_event("save", %{"message" => message}, socket) do
    changeset = %Message{body: ""} |> Message.changeset(%{})
    {:noreply, assign(socket, form: to_form(changeset))}
  end
end

I think it’s because you’ve got your form changeset in mount/1. You should use update/2 for that.

def update(%{faq: faq} = assigns, socket) do
    changeset = Content.change_faq(faq)

    {:ok,
     socket
     |> assign(assigns)
     |> assign_form(changeset)}
  end 

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
    assign(socket, :form, to_form(changeset))
end