Skip to content

Instantly share code, notes, and snippets.

@kraleppa
Last active March 30, 2026 09:21
Show Gist options
  • Select an option

  • Save kraleppa/641c7c43b753ff4fb1ca9625f7a38af4 to your computer and use it in GitHub Desktop.

Select an option

Save kraleppa/641c7c43b753ff4fb1ca9625f7a38af4 to your computer and use it in GitHub Desktop.
defmodule CounterLive do
use ShowcaseAppWeb, :live_view
use LiveStash
def mount(_params, _session, socket) do
socket
|> LiveStash.recover_state()
|> case do
{:recovered, recovered_socket} -> recovered_socket
_ -> assign(socket, count: 0)
end
|> then(&{:ok, &1})
end
def render(assigns) do
~H"""
<div>
<button phx-click="inc">+</button>
<span>{@count}</span>
</div>
"""
end
def handle_event("inc", _, socket) do
socket
|> assign(:count, socket.assigns.count + 1)
|> LiveStash.stash_assigns([:count])
|> then(&{:noreply, &1})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment