0

I have the following code for a simple live view which used to work on my other computer but i cannot seem to get it to work anymore:

defmodule ProjectWeb.CounterLive do
  use Phoenix.LiveView

  def mount(_session, socket) do
    socket = assign(socket, :count, 0)
    {:ok, socket}
  end

  def render(assigns) do
    ~L"""
      <h1>Count: <%= @count %></h1>
    """
  end
end
Snek
  • 141
  • 1
  • 7
  • 3
    `mount` is supposed to have 3 args. You only have 2. https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#c:mount/3 – Peaceful James Apr 09 '23 at 13:38

1 Answers1

0

If all setup with adding Phoenix LiveView and naming was right, then it seems like the problem is around mount's arity. There are some quotes from the lib:


mount(params, session, socket)
(optional)

@callback mount(
  params :: unsigned_params() | :not_mounted_at_router,
  session :: map(),
  socket :: Phoenix.LiveView.Socket.t()
) ::
  {:ok, Phoenix.LiveView.Socket.t()}
  | {:ok, Phoenix.LiveView.Socket.t(), keyword()}

The LiveView entry-point.

For each LiveView in the root of a template, mount/3 is invoked twice: once to do the initial page load and again to establish the live socket.

It expects three arguments:

    params - a map of string keys which contain public information that can be set by the user. The map contains the query params as well as any router path parameter. If the LiveView was not mounted at the router, this argument is the atom :not_mounted_at_router
    session - the connection session
    socket - the LiveView socket

It must return either {:ok, socket} or {:ok, socket, options}, where options is one of:

    :temporary_assigns - a keyword list of assigns that are temporary and must be reset to their value after every render. Note that once the value is reset, it won't be re-rendered again until it is explicitly assigned

    :layout - the optional layout to be used by the LiveView. Setting this option will override any layout previously set via Phoenix.LiveView.Router.live_session/2 or on use Phoenix.LiveView


Sabit Rakhim
  • 460
  • 3
  • 12