Liam_or
Is it still the case that you can only use one hook per Dom element in liveview? I know you can use one to delegate to others. But it would be way more modular and reusable if you can just have multiple hooks.
Liam ![]()
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
marcandre
Only one. You could write a generic one that delegates to others, or, if they are truly independent hooks, wrap your node in a
<div>with your second hook.Liam_or
Thanks for the info. What’s your take on a generic delegation? Are you thinking, for example, that if there was a need for pointerdown, pointerup, and pointermove listeners, you would have a ‘pointer’ hook that encompasses them all instead of individual hooks? If that’s the case I would end up with duplicates if I just wanted, say, a pointermove.
benwilson512
Those hooks sound entirely too granular to me. I would think of a hook like a component. The hook should capture all of the behavior of that component that needs to happen at the JS level. A given DOM element simply is only one component, it doesn’t make sense to be multiple. Composition would be accomplished by a component that can contain other components.
Liam_or
I understand when you say ‘think of a hook as a component’. But I can’t visualise what you mean by ‘contain other components’. Would you mind showing a super basic example?
marcandre
I think telling us your use case might be best for guidance.
A hook isn’t just “a listener to pointerdown, pointerup, and pointermove”, you want to do something with those, right?
Liam_or
Well, I’m trying to think more generally to understand the best approach to using hooks. I’d like to work on JS-heavy features, such as implementing drag-and-drop or drawing with SVGs. However, I want to ensure the code is modular and reusable, similar to how I would structure it with vanilla JavaScript or a framework like Svelte.
For instance, in a drag-and-drop feature, I might have functions that process pointer events, retrieve the client X/Y coordinates, and transform that data into a specific unit. In Svelte, I would make these reusable functions since I might use them frequently across my codebase.
If I combine everything into a single component-style hook, I risk ending up with redundant or repeated code. So I’m looking for general advice on how I can best structure hooks to maintain modularity and reusability while avoiding code repetition
marcandre
You may be confusing how to structure your JS side code into “libraries” (functions / classes / etc) and how to structure your hooks. Your hooks are the end-users of your libraries, not your libraries themselves.
I’d recommend to start with actual code and restructure later. You might get lost in remaining very abstract.
Good luck
Liam_or
That’s actually very helpful. Thank you
rhcarvalho
I was asking myself the same thing today.
As a use case, two existing hooks that made sense using in a single container: “size observer” and “chart renderer”.
The suggestions in previous answers are still viable:
But with LiveView 1.1, another possibility is using ColocatedHooks for one-of “composition”. Composing two existing hooks is probably too cumbersome, it’s probably a better idea to have the functionality separate from hooks, and hooks only call functions in reaction to lifecycle events (mount, update, destroy, etc).
Colocated or not, the “chart renderer” could also just use the functionality of the “size observer” so that we only need a single
phx-hookattribute.tfwright
Right, I don’t think colocated hooks are exactly relevant here. The confusion is caused by thinking of hooks as units of abstraction, because they are not that, they are just javascript “controllers” for your DOM, which already defines the units of abstraction. They expose an API for triggering behavior on rendering events, not injecting specific logic.
I don’t think OP ever shared details about the original use case beyond “size observer“ and “chart renderer,” but presumably those would be modules that a single hook on a component that uses both features would import, either in app.js or in the component itself in the case of collocated hooks.
I am tempted to wonder whether forcing them to be component specific in some way would have made this clearer. Or, if allowing multiple hooks per component was really desirable, just having
phx-mount/phx-updateattribute, but then I don’t know how the more advanced features would work.