How can I run the handle_event("open") function defined in MyAppWeb.LiveModal with a button defined in MyAppWeb.Index? I have tried using target, but it does not even print “open sesame”.
defmodule MyAppWeb.Index do
...
def render(assigns) do
<button
phx-click={JS.push("open", target: "#modal")}
id="modal-btn"
>
Invite
</button>
<.live_component module={CollaborlistWeb.Live.InviteModal} id="modal" />
end
...
defmodule MyAppWeb.LiveModal do
use CollaborlistWeb, :live_component
@impl true
def mount(socket) do
{:ok, socket |> assign(open: false)}
end
@impl true
def handle_event("open", _, socket) do
IO.puts("open sesame")
{:noreply, assign(socket, open: true)}
end
@impl true
def render(assigns) do
~H"""
<div>
<%= if @open do %>
OPEN
<% else %>
CLOSED
<% end %>
</div>
"""
end
end
No javascript error in the console? (like the LiveView socket disconnecting)
Did you try to turn your light on & off three times in a row ? … it helps sometimes
I forget where, but in the LiveComponent docs it mentions that an id passed to the live_component function is used for tracking in in the socket, but it sets the id assign in the component so you can choose to also use it on the root function. You don’t have to hardcode it