Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

benkaiser
Copy link

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.

index.js Outdated
element.current.removeEventListener(('mouseup'), mouseUp)
element.current.removeEventListener(('mouseleave'), mouseLeave)
}
}, [preventSwipe])
Copy link
Author

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 })
Copy link
Author

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.

Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant