You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/client/js-interop.md
+50-4Lines changed: 50 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -159,10 +159,11 @@ or removed by the server, a hook object may be provided via `phx-hook`.
159
159
*`disconnected` - the element's parent LiveView has disconnected from the server
160
160
*`reconnected` - the element's parent LiveView has reconnected to the server
161
161
162
-
*Note:* When using hooks outside the context of a LiveView, `mounted` is the only
163
-
callback invoked, and only those elements on the page at DOM ready will be tracked.
164
-
For dynamic tracking of the DOM as elements are added, removed, and updated, a LiveView
165
-
should be used.
162
+
*Note:* hooks also run on regular pages that are *not* LiveViews — see
163
+
[Hooks and JS commands outside of a LiveView](#hooks-and-js-commands-outside-of-a-liveview).
164
+
In that case, `mounted` is the only callback invoked, and only those elements on the page
165
+
at DOM ready will be tracked. For dynamic tracking of the DOM as elements are added,
166
+
removed, and updated, a LiveView should be used.
166
167
167
168
The above life-cycle callbacks have in-scope access to the following attributes:
168
169
@@ -498,3 +499,48 @@ Hooks.MyHook = {
498
499
Setting IDs directly via `node.id = "..."` or other direct DOM manipulation methods will cause DOM patching issues. Always use `js().setAttribute()` instead.
499
500
500
501
If the server has already assigned an ID to an element, you cannot replace it with a different ID from the client side. Client-side IDs should only be set on elements that have no server-assigned ID.
502
+
503
+
## Hooks and JS commands outside of a LiveView
504
+
505
+
Hooks (`phx-hook`) and `Phoenix.LiveView.JS` commands are not exclusive to LiveViews.
506
+
They also work on regular pages [rendered by a normal Phoenix
507
+
controller](https://hexdocs.pm/phoenix/controllers.html#rendering) — pages with no
508
+
live connection (sometimes referred to as *dead views*, *dead renders*,
509
+
*static pages*, or simply markup *outside of a LiveView*).
510
+
511
+
This includes markup that lives *outside* the live container on a page that does have a
512
+
LiveView, such as your root layout — those elements belong to a body-level regular view,
513
+
not to the LiveView.
514
+
515
+
To enable it, the page must load and connect `LiveSocket`, exactly as a LiveView page does:
0 commit comments