Giter Site home page Giter Site logo

Comments (8)

srmagura avatar srmagura commented on June 4, 2024

This seems like it could be an antipattern. Wouldn't it be simpler if window always had a mouseup event handler? If mouseup occurs but mouseDown == false, the mouseup handler would just do nothing.

If I am missing something about your use case, feel free to explain.

from use-event-listener.

cmdcolin avatar cmdcolin commented on June 4, 2024

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

from use-event-listener.

cmdcolin avatar cmdcolin commented on June 4, 2024

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

from use-event-listener.

cmdcolin avatar cmdcolin commented on June 4, 2024

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

from use-event-listener.

srmagura avatar srmagura commented on June 4, 2024

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 mouseup handlers.

It would be nice if you could do:

const [mouseDown, setMouseDown] = useState(false)

function mouseUpHandler() { ... }

useEventListener('mouseup', mouseDown ? mouseUpHandler : undefined)

return <div onMouseDown={() => setMouseDown(true)}/>

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 mouseup event could occur before the React component rerenders, in which cause mouseUpHandler would not be called. Even if this works in today's React, it might stop working when React starts doing async rendering. What are you thoughts here?

from use-event-listener.

cmdcolin avatar cmdcolin commented on June 4, 2024

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

useEventListener('mouseup', () => {
    if(mouseDown) {
        mouseUpHandler()
    }
}, [mouseDown])

Note: haven't really fully thought it through...just copying the useEffect style of thought :)

from use-event-listener.

srmagura avatar srmagura commented on June 4, 2024

useEventListener always uses the latest event handler function you provide, so I don't see the benefit of passing a dependency array.

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 useEventListener conditional, we can discuss further.

from use-event-listener.

cmdcolin avatar cmdcolin commented on June 4, 2024

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)

from use-event-listener.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.