How to get javascript params in LiveView?
I found out that Phoenix LiveView has a function get_connect_params to be used when you want to get some value sent at the connection time from the javascript.
This way I can send params like:
// app.js
const params = {
_csrf_token: csrfToken,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
const liveSocket = new LiveSocket('/live', Socket, { params });
And get those like on my LiveView:
# my_live_view.ex
@impl true
def mount(_params, _session, socket) do
tz = get_connect_params(socket)["timezone"] || "America/New_York"
{:ok, assign(socket, :timezone, tz)}
end
Tweet