-
Notifications
You must be signed in to change notification settings - Fork 112
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
Recreate event listener functions on preventSwipe change #31
base: master
Are you sure you want to change the base?
Conversation
index.js
Outdated
element.current.removeEventListener(('mouseup'), mouseUp) | ||
element.current.removeEventListener(('mouseleave'), mouseLeave) | ||
} | ||
}, [preventSwipe]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@3DJakob should we add all of the props to this dependency list so they reference the latest handleSwipeReleased
in the event listeners? I think this bug extends to the other props but I'm only changing preventSwipe (hence why I noticed the issue)
let speed = { x: 0, y: 0 } | ||
let lastLocation = { x: 0, y: 0, time: new Date().getTime() } | ||
let mouseIsClicked = false | ||
const [offset, setOffset] = React.useState({ x: null, y: null }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@3DJakob needed to move these to state, since they would get 'reset' every time useLayoutEffect was called. So if it got called on an element you were mid-way through dragging then the element would jump to the wrong position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'm open to better options that don't require re-calling useLayoutEffect
many times)
To ensure that changing preventSwipe prop in subsequent renders behaves correctly, we need to re-generate the event listeners so they use the latest
handleSwipeReleased
callback.