Skip to content

Instantly share code, notes, and snippets.

@nicolasblanco
Created January 19, 2023 14:19
Show Gist options
  • Save nicolasblanco/cdd7968079f3e79037359949c60bd38c to your computer and use it in GitHub Desktop.
Save nicolasblanco/cdd7968079f3e79037359949c60bd38c to your computer and use it in GitHub Desktop.
defmodule MyTwitterWeb.CounterLive do
use MyTwitterWeb, :live_view
def render(assigns) do
~H"""
<div>
<.button phx-click="dec">-</.button>
<%= @counter %>
<.button phx-click="inc">+</.button>
</div>
"""
end
def mount(_params, _session, socket) do
socket =
socket
|> assign(:counter, 10)
{:ok, socket}
end
def handle_event("dec", _, socket) do
{:noreply, update(socket, :counter, fn count -> count - 1 end)}
end
def handle_event("inc", _, socket) do
{:noreply, update(socket, :counter, fn count -> count + 1 end)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment