Created
August 24, 2021 20:39
-
-
Save kidbombay/eee237b4e109fb32a297c71062574c8d to your computer and use it in GitHub Desktop.
Getting Live View to work with Javascript Hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### In App.js initialize the hook and set it on the LiveSocket | |
### Tested with LiveView 0.15.7 | |
``` | |
Hooks.MyHook = { | |
mounted() { | |
console.log("MyHook.mounted") | |
}, | |
updated() { | |
console.log("MyHook.updated") | |
} | |
} | |
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") | |
let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}}) | |
``` | |
### In Your Leex file, add the attribute for the hook. This can go on any element that will be changed via the LiveView. | |
``` | |
<div id="chat-messages" phx-hook="MyHook"> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment