View Source Rephex.LiveComponent (rephex v0.1.1)

Implement utility functions and handling functions for Rephex by use Rephex.LiveComponent.

Example

defmodule ExampleWeb.AccountLive.ComponentA do
  use ExampleWeb, :live_component
  use Rephex.LiveComponent

  @initial_local_state %{
    count_double: 0
  }

  @impl true
  def mount(%Socket{} = socket) do
    {:ok, socket |> assign(@initial_local_state)}
  end

  @impl true
  def update(assigns, socket) do
    {:ok,
    socket
    |> propagate_rephex(assigns)
    |> assign(:count_double, assigns.rpx.count * 2)}
  end

  def handle_event("event_in_component", params, socket) do
    # Mutate Rephex state in root LiveView
    {:noreply,
    socket
    |> call_in_root(fn state -> mutation(state) end)}
  end
end

Summary

Functions

Call function in root LiveView.

Assign rephex state to socket assigns in LiveComponent. WARN: Do NOT use this function in LiveView (root).

Functions

@spec call_in_root(v, (Phoenix.LiveView.Socket.t() -> Phoenix.LiveView.Socket.t())) ::
  v
when v: any()

Call function in root LiveView.

Example:

def handle_event("event_in_component", params, socket) do
  {:noreply,
  socket
  |> call_in_root(fn state -> mutation(state) end)}
end
Link to this function

propagate_rephex(socket, assigns)

View Source
@spec propagate_rephex(Phoenix.LiveView.Socket.t(), map()) ::
  Phoenix.LiveView.Socket.t()

Assign rephex state to socket assigns in LiveComponent. WARN: Do NOT use this function in LiveView (root).

Example:

def update(%{rpx: _} = assigns, socket) do
  {:ok,
  socket
  |> propagate_rephex(assigns)
  |> assign(other_state)}
end