I have forms in my app modeled using LiveComponents (they’re in modals) that mostly manage their own state. The following is an example of one:
<.live_component
module={TransactionForm}
id="..."
transaction={...}
open={@live_action in [:create, :edit]}
on_success={&push_patch(...)}
on_cancel={JS.patch(...)}
/>
The live component will fetch data in the mount
callback that’s required for things like select inputs (e.g. list of merchants, accounts, etc.).
I haven’t really figured out a good pattern to handle refreshing of data managed by live components. For example, if I have another form in the live view that creates a new merchant, I would want the other forms in the page to refresh their state to reflect the new merchant. It’s easy to do this at the live view level using Phoenix PubSub. I can emit an event when the change is triggered and refresh the live view’s state in a handle_info
callback.
Since live components don’t have this capability, I’ve identified a few alternatives:
-
Manage all this state at the live view level. I tried this but it makes the component really hard to work with since it ends up requiring each live view to load a bunch of state just to pass through to the live component. To me it kind of defeats the point of the live component in the first place.
-
Load data in update callback instead of on mount. This will ensure that the live component will always have the latest data since it will just refetch it every time it opens / closes. The downside is that most of the time the refetching was not needed.
-
Use
send_update
as a mechanism to send an event to the live component to tell it to reload state. I don’t like the idea of the caller literally settings assigns that it shouldn’t otherwise know about, but I did consider exposing a function from the live component module that could be called in the live view’shandle_info
callback. It’s a little manual, but probably would work? An example:
defmodule TransactionForm do
def refresh_data(id) do
merchants = ...
send_update(pid, id, merchants: ...)
end
end
If live components could handle their own events I think that would solve the problem for me, but I understand the limitations of that given they share the process with the parent live view. Very interested in hearing how others are addressing this problem!