I did side effects at the mutation stage, could this be the source of the issue?
I’m sorry, I got that wrong back in the day. The state should only be changed in the Mutation
and not the Action
-Handler (which is the handle_info
-function). Please try again according to the updated Readme. However, I’m not sure whether this will fix your problem with the detection of the updated store.
In order for the inner components to detect a state change, they need to get the state passed into them by the parent LiveView. So, if your store looks like this:
defmodule MyStore do
use LiveEx
@initial_store %{
content: nil
}
def init(socket), do: init(@initial_store, socket)
def handle_info(%{type: "set_content"} = action, socket) do
commit("set_content", action.payload, socket)
end
def set_content(payload, socket) do
assign(socket, :content, payload)
end
end
You will need to pass the content
to the live_component
like this:
# In your LiveView .leex template
<%= live_component, MyLiveComponent, content: @content %>
Also, if you overwrote the update(assigns, socket)
function in your MyLiveComponent
, make sure that you’re actually setting the content
assign. Like this:
defmodule MyLiveComponent do
use Phoenix.LiveComponent
def render(assigns) do
~L"""
<div><%= @content %></div>
"""
end
def update(assigns, socket) do
{:ok, assign(socket, :content, assigns.content)}
end
end