Phoenix LiveView mount does not get called

I have tried creating a fresh project using LiveView. I am following this tutorial : https://www.youtube.com/watch?v=qpaFivCmJOY. I have followed the installation instruction manual and created a basic controller for my view, that contains a live_render tag, referencing a LiveView controller.

Template :

<h1>Projects</h1>                                                                                                                                                                                                                                                           
<%= live_render(@conn, TodoistHelperWeb.AppController) %>

Normal controller :

defmodule TodoistHelperWeb.PageController do                                                                                                                                                                                                                                
  use TodoistHelperWeb, :controller                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                            
  def index(conn, _params) do                                                                                                                                                                                                                                               
    render(conn, "index.html")                                                                                                                                                                                                                                              
  end                                                                                                                                                                                                                                                                       
end 

LiveView controller :

defmodule TodoistHelperWeb.AppController do                                                                                                                                                                                                                                 
  use Phoenix.LiveView                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                            
  def mount(_session, socket) do                                                                                                                                                                                                                                            
    {:ok, assign(socket, projects: ["project1"])}                                                                                                                                                                                                                                  
  end                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                            
  def render(assigns) do                                                                                                                                                                                                                                                    
      TodoistHelperWeb.AppView.render("projects.html", assigns)                                                                                                                                                                                                             
  end                                                                                                                                                                                                                                                                       
end

LiveView template :

<%= for project <- @projects do %>                                                                                                                                                                                                                                          
<div><%= project %></div>                                                                                                                                                                                                                                                   
<% end %>

The project compiles correctly, everything seems to work, but the @projects variable cannot be found in the liveview template! If I replace it with a static list, the page is displayed without any errors. Using print statements, I was able to determine that render is properly called, however mount is not.

What did I miss? Did I mess up one of the installation steps?

It should be mount/3:

@impl true
def mount(_params, _session, socket) do
  {:ok, assign(socket, projects: ["project1"])}
end

If you put @impl true whenever you are implementing a callback, Elixir will let you know if you got the wrong name or the wrong number of arguments. :slight_smile:

4 Likes

Oh god that was pretty dump -_-… Thanks!