4
votes

I'm writing a ClockComponent to learn about Phoenix LiveComponents. I almost have it, but it's sending the :tick message to its parent. How can I get it to send that message to itself? I wanted to use myself() instead of self() but apparently that's not a thing.

defmodule ClockComponent do
  use Phoenix.LiveComponent

  @impl true
  def mount(socket) do
    if connected?(socket), do: :timer.send_interval(1000, self(), :tick)
    {:ok, assign(socket, date: :calendar.local_time())}
  end

  def handle_info(:tick, socket) do
    {:noreply, assign(socket, date: :calendar.local_time())}
  end

  @impl true
  def render(assigns) do
    ~L"""
    The time is <%= @date |> Timex.to_datetime("America/Denver") |> Timex.format!("{RFC1123}") %>
    """
  end
end
1

1 Answers

4
votes

I don't think you can based on this: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-managing-state

Components run inside the LiveView process, but may have their own state and event handling.

I believe both "child component" and parent share the same process.