Props & data
Async Assigns
assign_async's AsyncResult reaches React as loading, ok and failed
- assign_async
- AsyncResult encoder
Key concepts
assign_async/3
in mount/3
starts work in a linked process and immediately returns a
Phoenix.LiveView.AsyncResult
in its loading state. When the work finishes, LiveView updates the assign for
you — either to a successful result or to a failure — with no handle_info/2
to write.
Loading…
How it works
The AsyncResult
struct is passed straight to React as a prop, unchanged, because LiveReact
ships an encoder for it. React reads loading, ok
and failed
directly off the prop rather than the server flattening them into three
separate assigns and deciding which one to send.
"Reload" re-runs the same async work; "Simulate failure" reruns it in a
mode that returns {:error, reason}
instead, so all three states — loading, ok and failed — are reachable from
the page itself.