View Source Rephex.State (rephex v0.1.1)
Define Rephex state by use
.
Defined state must be initialized by init/1
in Phoenix.LiveView.mount/3
.
Example
defmodule ExampleWeb.State do
@type t :: %{
count: integer(),
add_twice_async: %AsyncResult{}
}
@initial_state %{
count: 0,
}
use Rephex.State, initial_state: @initial_state
def add_count(socket, %{amount: amount} = _payload) when is_integer(amount) do
# You can use `update_state`, `update_state_in` and `put_state_in` to update state
update_state_in(socket, [:count], &(&1 + amount))
end
end
defmodule ExampleWeb.AccountLive.Index do
use ExampleWeb, :live_view
use Rephex.LiveView
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> ExampleWeb.State.init()}
end
end