Patterns for displaying errors to users with LiveView

Flash is nice for redirecting away from the LV, but for showing the user feedback as they are interacting with your LV, you can simply lean on the existing features:

def render(assigns) do
  ~L"""
  <%= if @notice do %>
    <h2><%= @notice %>
  <% end %>
  ...
  """
end

def mount(_, socket) do
  {:ok, assign(socket, ..., notice: nil)}
end
...
def handle_event(...) do
  case some_thing(...) do
    ... ->
    {:error, _} -> {:noreply, assign(socket, :notice, "it didn't work")}
  end
end

That’s all there is to it, no extra pattern required :slight_smile:

11 Likes