Use data in handle_event from mount

In live view I am loading some data in mount/3, I want to then access this data later on in handle_event however I am not sure how to share it, do I put it on the session, the connection, or something else?

I feel I am missing something obvious. Thanks.

You keep your state in socket.assigns:

def mount(_params, _session, socket) do
  {:ok, assign(socket, :my_data, [1, 2, 3])}

def handle_event("some-event", _payload, socket) do
  IO.inspecdt({:my_data, socket.assigns.my_data})
  {:noreply, socket}

Thanks for the answer, just what I needed. I realised I needed to also pass this data through to form components which got it working for me.

1 Like