Liveview lifecycle hooks - mount or on_mount?

According to Phoenix.LiveView — Phoenix LiveView v0.17.5, if I use:

live_session :default, on_mount: MyAppWeb.InitAssigns do

Then in MyAppWeb.InitAssigns I should define a on_mount function like:

def on_mount(:default, _params, _session, socket) do

But if I do this:

live_session :default, on_mount: RoboticaFaceWeb.InitAssigns do

I get an error that the mount function is not defined:

Erlang/OTP 24 [erts-12.0.4] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit]

Compiling 23 files (.ex)
warning: RoboticaFaceWeb.InitAssigns.mount/3 is undefined or private
Found at 2 locations:
  lib/robotica_face_web/router.ex:1: RoboticaFaceWeb.Router.__match_route__/3
  lib/robotica_face_web/router.ex:1: RoboticaFaceWeb.Router.__routes__/0

Compilation failed due to warnings while using the --warnings-as-errors option

Instead of a on_mount function I have to define a mount function:

def mount(_params, session, socket) do

Then it works perfectly.

Just wondering if I have misunderstood something about the docs???

Make sure your LiveView version matches the version you are reading the docs for. The mount callbacks were redesigned on v0.17.x.

2 Likes

Whoops. Sorry, didn’t realize I was now on an old version.