Search code examples
elixirphoenix-frameworkphoenix-live-view

Passing a value from client to Phoenix server using LiveView during mount


I'm trying to pass a value from the browser (e.g., localstorage) to the server, and make it available as the live template (leex) is mounted and UI view is created. Tried the following only to get the message shown below.

<JS>
let liveSocket = new LiveSocket("/live", Socket, {params: {init_state: "value from localstorage"}..

<Phoenix>
def mount(params, _session, socket) do
  IO.inspect(params)  # this returns "not route mounted"

Solution

  • get_connect_params/1 appears to be the way to do it according to this issue page (https://github.com/phoenixframework/phoenix_live_view/issues/204). Also it's the socket parameter, not params to look for the variable under.

    <JS>
    let liveSocket = new LiveSocket("/live", Socket, {params: {init_state: "value from localstorage"}..
    
    <Phoenix>
    def mount(_params, _session, socket) do
      IO.inspect(get_connect_params(socket)["init_state"])