legibly-sinless-fil
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?
Marked As Solved
josevalim
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. ![]()
Last Post!
legibly-sinless-fil
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









