sodapopcan
Is there a way to pass initial values to a LiveView JS hook from the server?
Here’s my example LiveView:
defmodule FooWeb.PageLive do
use FooWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, foo_assigns_i_want_to_reference_in_my_hook: %{bar: "hi"})}
end
@impl true
def render(assigns) do
~L"""
<div phx-hook="FooDomNode"></div>
"""
end
end
And here is my JS hook:
let Hook = {}
Hook.FooDomNode = {
mounted() {
// Is there anyway I can access `foo_assigns_i_want_to_reference_in_my_hook` in here?
}
}
// ... More JS boilerplate that properly passes `Hook` to `new LiveSocket(...)` and connects
As the commented part of the JS hook states: Is there anyway to reference the assigns of the LiveView mount in the JS hook’s mounted callback?
Thank you!!
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
I’m going to preemptively add: I’m wondering if there is a cleaner and more official way to do this than to serialize the assigns in a
dataattribute on the hook’s node.sfusato
There was recently added
push_event/3in 0.14.0 for pushing events and data from the server to the client.sodapopcan
Oh, great, thank you! It took me a minute to realize I was on v1.13 of LiveView (I was conflating Phoenix and LiveView versions).
Lango
How exactly would this work? I’m assuming the answer to the question is ‘no’ you can’t access it from the mounted event?
In my mind I would need to do something like this:
Is that correct? Or am I way off base here
cmo
There are a few ways of communicating back and forth. The aforementioned
push_event, the aforementioned assigning to a data attribute and reading it, e.g.and hook:
You see the
data-valueattribute in the render function is picked up in thevalue()function on the hook.data-valuein the Hook’s tag is picked up bythis.el.dataset.value.See Phoenix LiveDashboard chart code which does this.
You can then send things back to the server with
pushEventandpushEventToif you need.Something like this:
Lango
Thanks for that cmo. Apologies for taking so long to reply, I was expecting to be email notified that someone replied.
I had a large amount of trouble getting complex objects to pass in via the data-value.
Instead I think I’m going to trigger a resend to everyone when a new client joins. That would be acceptable for my scenario