Better way of reusing event listeners for hooks
I have a hook for the window size.
It works great, but I want to reuse my event listener because I'll be using this hook in many components. I can always do something like below, but there has to be a better way to do it with something that SolidJS provids? Or even JS.
3 Replies
tbf though the first one is good enough. I don't see why you should optimize.
Your probably right. It is much more concise and readable.
I have a global monitor for window size that's used by all sorts of stuff. I didn't want to add potentially dozens/hundreds of eventListeners onto the Window, so I've got a single use of your first example in a store component which delivers a reactive object of
{w:number, h:number}
almost globally via context.