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/1 is 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
      <%= @greeting %>

  def mount(_params, _session, socket), do: {:ok, socket}

  def handle_params(_params, _url, socket) do
    {:noreply, assign(socket, :greeting, "Hello World")}

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:

And if I understand this comment correctly, then the life-cycle is shorter for live navigation (with e.g. live_patch or live_redirect:


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.


1 Like

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 :slight_smile:

1 Like