Skip to content

Instantly share code, notes, and snippets.

@brianstorti
Created April 20, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianstorti/8dea2107d7bdff8d7ab93e61c3dff1c7 to your computer and use it in GitHub Desktop.
Save brianstorti/8dea2107d7bdff8d7ab93e61c3dff1c7 to your computer and use it in GitHub Desktop.
defmodule CounterWeb.Counter do
use Phoenix.LiveView
def render(assigns) do
~L"""
<div>
<h1>The count is: <%= @val %></h1>
<button phx-click="dec">-</button>
<button phx-click="inc">+</button>
</div>
"""
end
def mount(_params, _session, socket) do
{:ok, assign(socket, val: 0)}
end
def handle_event(action = "inc", _, socket) do
{:noreply, update(socket, :val, &(&1 + 1))}
end
def handle_event(action = "dec", _, socket) do
{:noreply, update(socket, :val, &(&1 - 1))}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment