I have an problem regarding sending messages from an child live_component to liveview parent. As i understand, the live_component will live in the same process as the liveview.
Definition of multiselectcomponent
defmodule FormTestWeb.MultiSelectComponent do
use FormTestWeb, :live_component
def render(assigns) do
~H"""
<div class="flex justify-center">
<%= inputs_for @form, :options, fn value -> %>
<div class="form-check">
<%= checkbox( value, :selected, phx_target: @myself, phx_change: "checked", value: value.data.selected) %>
<%= label( value, :label, value.data.label, class: "ml-2") %>
<%= hidden_input( value, :label, value: value.data.label) %>
</div>
<% end %>
</div>
"""
end
def handle_event("checked", %{"navigate" => %{"options" => values}}, socket) do
[{index, %{"selected" => selected?}}] = Map.to_list(values)
index = String.to_integer(index)
current_option = Enum.at(socket.assigns.options, index)
updated_options =
List.replace_at(socket.assigns.options, index, %{current_option | selected: selected?})
Process.send(self(), {:updated_options, updated_options})
{:noreply, socket}
end
def update(params, socket) do
%{options: options, form: form, id: id} = params
socket =
socket
|> assign(:id, id)
|> assign(:options, options)
|> assign(:form, form)
{:ok, socket}
end
end
The Process.send(self(), {:updated_options, updated_options}) will give the error function Process.send/2 is undefined or private but still send the message {:updated_options, updated_options} to liveview.
The liveview catches the event through handle_event:
def handle_event("validate", %{"navigate" => navigate}, socket) do
{:noreply, socket}
end
In my understanding, using the send/2 function should send message to parent liveview, in which will catch such message with handle_info/2. But nothing is happening.
When i use Process.send/2, it crashes but still sends the message after the crash. When i correct it and use Process.send/3. Nothing is happening.
Any suggestions what might have gone wrong?