PJUllrich
Understanding the LiveView life-cycle
I’m wondering at which point the handle_params/3-callback is invoked in the LiveView life-cycle. I think it is after mount/3, but before render/1. However, the LiveView docs say:
After mounting,
render/1is invoked and the HTML is sent as a regular HTML response to the client. Link
I believe that render/1 is not immediately called after mount/3 though, but that handle_params/3 is called in between. Am I correct to assume this?
I tested it out with the following LiveView:
defmodule TestLive do
render(assigns) do
~H"""
<%= @greeting %>
"""
end
def mount(_params, _session, socket), do: {:ok, socket}
def handle_params(_params, _url, socket) do
{:noreply, assign(socket, :greeting, "Hello World")}
end
end
This renders just fine, which leads me to assume that handle_params/3 is called after mount/3 and before render/1. Otherwise, the @greeting assign wouldn’t be available in render/1.
So, the life-cycle must be:
mount/3 → handle_params/3 → render/1
And if I understand this comment correctly, then the life-cycle is shorter for live navigation (with e.g. live_patch or live_redirect:
handle_params/3 → render/1
If this is the case, then I’ll try to write a PR to re-write the docs to clarify this.
PS: After I wrote this, I realised that I could just put IO.inspects in the callbacks and indeed, the functions were called as explained above.
Marked As Solved
APB9785
Also Liked
PJUllrich
Ah thank you! I didn’t see this guide, but only read the module documentation.
This doc explains is quite well actually. So, no PR needed I guess ![]()
Popular in Questions
Other popular topics
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








