Skip to content

Instantly share code, notes, and snippets.

@kidbombay
Created August 24, 2021 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidbombay/eee237b4e109fb32a297c71062574c8d to your computer and use it in GitHub Desktop.
Save kidbombay/eee237b4e109fb32a297c71062574c8d to your computer and use it in GitHub Desktop.
Getting Live View to work with Javascript Hooks
### 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