What is the `__live__` key for? I'm getting "(KeyError) key :__live__ not found" with Phoenix.LiveComponent

I have what I think is a super-simple live component that I’m calling in a straightforward LiveView:

First, the code below works fine, and I see “hi tadasajon” in a red outlined div:

# router.ex
live("/test", TestLive, :index)

# test_live.ex
defmodule MyAppWeb.TestLive do
  use MyAppWeb, :live_view

  def render(assigns) do
    ~H"""
    <div class="outline-red p-4 m-4">
        hi tadasajon
    </div>
    """
  end
end

Now I want to add a live component, so I have done this:

# router.ex
live("/test", TestLive, :index)

# test_live.ex
defmodule MyAppWeb.TestLive do
  use MyAppWeb, :live_view

  def render(assigns) do
    ~H"""
    <div class="outline-red p-4 m-4">
        hi tadasajon
        <.live_component id="unique-id" module={MyAppWeb.TestLiveComponent} />
    </div>
    """
  end
end

# test_live_component.ex
defmodule MyAppWeb.TestLiveComponent do
  use Phoenix.LiveComponent

  def render(assigns) do
    ~H"""
    <div> hello again, tadasajon </div>
    """
  end
end

The error I am getting is:

** (KeyError) key :__live__ not found in: %{__changed__: nil, id: "unique--id", module: MyAppWeb.TestLiveComponent}

Further to our slack conversation, this error happens when you use the 0.17 syntax on 0.16.

Either upgrade liveview or switch your component invocation to:

<%= live_component MyAppWeb.TestLiveComponent, id: "my-unique-component" %>

As you can see in the old syntax the first argument was the component itself, and this has the __live__ key thats expected, the new syntax instead passes just a map but thats not compatible version 0.16.

5 Likes

For reference in the future if people search for this error message and end up finding this thread. I had the right syntax in a project and still got this message for some reason. Couldn’t find any mistake from my code. In the end I removed the _build folder and restarted the project, and that fixed it.

So if you’re already on 0.17 and getting this error, try nuking your build folder and recompiling.