Last active
March 30, 2026 09:21
-
-
Save kraleppa/641c7c43b753ff4fb1ca9625f7a38af4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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