Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Events with phx-target={@myself} in LiveViewNative.LiveComponent are routed to parent LiveView #238

@zeadhani

Description

@zeadhani

I've encountered an issue where events from a LiveViewNative.LiveComponent are being routed to the parent LiveView instead of being handled by the component itself, despite using phx-target={@myself} in the component template.

Steps to reproduce:

  1. Create a LiveViewNative.LiveComponent with an event handler
  2. Add a button with phx-click and phx-target={@myself}
  3. Implement the corresponding handle_event/3 function in the component
  4. Observe that the event is sent to the parent LiveView instead of being handled by the component

Code example:

# Component definition
defmodule MyApp.Home.MainLive do
  use LiveViewNative.LiveComponent,
    format: :swiftui

  def render(assigns, _interface) do
    ~LVN"""
    <VStack>
      <Text>Button Component</Text>
      <Text>Count: <%= @count %></Text>
      <Button id="component-button" phx-click="increment" phx-target={@myself}>
        <Text>Click to increment</Text>
      </Button>
    </VStack>
    """
  end
  
  def mount(socket) do
    {:ok, assign(socket, count: 0)}
  end
  
  def update(assigns, socket) do
    {:ok, socket |> assign(assigns)}
  end
  
  def handle_event("increment", _params, socket) do
    IO.puts("*** Component received increment event ***")
    {:noreply, update(socket, :count, &(&1 + 1))}
  end
end

# Parent LiveView SwiftUI renderer
defmodule MyApp.HomeLive.SwiftUI do
  use LiveViewNative.Component,
    format: :swiftui,
    as: :render
    
  def render(assigns, _interface) do
    ~LVN"""
    <VStack>
      <Text font="title">LiveView Native Example</Text>
      <.live_component
        module={MyApp.Home.MainLive}
        id="simple-component"
      />
    </VStack>
    """
  end
end

Expected behavior:

The handle_event/3 function in the component should receive and handle the "increment" event.

Actual behavior:

The event is routed to the parent LiveView

This behavior differs from standard Phoenix LiveView where phx-target={@myself} correctly routes events to the component's handler.

Environment:

  • LiveView Native version: 0.4.0-rc.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions