-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only start listener after some condition? #30
Comments
This seems like it could be an antipattern. Wouldn't it be simpler if If I am missing something about your use case, feel free to explain. |
The basic idea would be to avoid maybe having unnecessary mouseup handlers if there are many elements on the screen, but also sort of avoid having unnecessary logic in my mouseup handler to make it relevant only when a sort of "click and drag" is happening This sometimes occurs if I have like say, a slider <-------> I start by clicking on the dashes Then I quickly drag my mouse to the right and it goes fully off the slider but I still want to capture the mouse up, so I add a global event handler to make this work |
The use case I have isn't specifically for slider but you can see slider app using this type of thing here https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Slider/Slider.js |
It might be outside the scope of this repo but I was just thinking...if there was a way to simplify this type of logic, it would be pretty nice |
Cool, I see what you are trying to accomplish now. You really just want a more straightforward way to write the code and you want peace of mind that there is no performance degradation due to having many unnecessary It would be nice if you could do:
Is this how you want it to work? (this doesn't work currently) I was going to go ahead and implement this new feature, but now I'm concerned about a race condition. The |
It does seem like these is a danger in using conditionals in hooks like this. It seems like maybe it would be ideal to have a dependency array type thing a la useeffect
Note: haven't really fully thought it through...just copying the useEffect style of thought :) |
Adding this feature seems deceptively complicated because of potential race conditions. I'm going to close the issue for now since there is an easy (if not ideal) workaround. If you can think of good way to make |
Just in case anyone comes across this, my original case (adding a global mouse up and mouse move handler after a mouse down) is actually nicely solved by pointerEvents (just setPointerCapture and then pointMove and pointerUp do not have to be global, see https://codesandbox.io/s/ox5lx949oq) |
I am not sure if this is a great use case but sometimes we have code like this
Do you thing it's worth it to have a conditional use-event-listener for a case such as this? Or do you think code like this is maybe an antipattern
The text was updated successfully, but these errors were encountered: