VTek
Where do hooks go into the DOM?
1st project here. Trying to attach a hook. I’ve spent hours logging everything I can think of to figure out why I can see my hook mount and even verify it mounts to the id I gave the element, but nothing I do will fire it.
Is there a way to trigger it from F12 console to confirm it’s even there? I tried this, but get true, and no console.log that I’m expecting:
hookElement.dispatchEvent(new CustomEvent("phx:update"))
TL;DR but here’s the now obnoxious dysfunctional hook (and yes I do send it when I new LiveSocket):
console.log("Hook definition reached");
let Hooks = {};
Hooks.IntroductionComplete = {
mounted() {
console.log("IntroductionComplete hook mounted on " + this.el.id);
this.lastEventTime = 0;
this.setupEvent();
if (this.el) {
this.el.dispatchEvent(new CustomEvent("phx:update"));
}
else console.log("No element found");
},
updated() {
console.log("IntroductionComplete hook updated");
this.setupEvent();
},
setupEvent() {
console.log("Before setting up introduction_complete event listener");
if (this.hasEventListener) return;
setTimeout(() => {
this.hasEventListener = true;
console.log("Setting up introduction_complete event listener");
this.handleEvent("introduction_complete", () => {
console.log("Entering introduction_complete event handler");
console.log("Introduction complete event received");
this.revealTextArea();
});
console.log("After setting up introduction_complete event listener");
}, 300); // Adjust the delay for testing
},
revealTextArea() {
console.log("Revealing text area");
// Implement logic to reveal or enable the text area here
}
};
Marked As Solved
LostKobrakai
That’s the incorrect part. You need socket = push_event(…).
Also Liked
regex.sh
You “dispatch” Hook by hooking it to an HTML element with id…
https://hexdocs.pm/phoenix_live_view/js-interop.html#client-hooks-via-phx-hook
Adzz
What are you trying to do with the hook first? revealTextArea implies showing / hiding something which can be done more simply either with CSS and HTML (eg a <detail> component) or with the JS functions like toggle.
If you just want to get a hook working start with something very simple that adds a class to change the color of the element clicked. That way you can proceed in steps and see if the problem is defining the hook, or what the hook does.
It doesn’t work, no matter where I put it. I call it like this and console logs tell me it’s called
This implies that the hook is “hooked up” correctly, suggesting that what you have written in the hook doesn’t do what you expect it to do.







