Skip to content

Commit

Permalink
fix(dragging): Pooled events to reuse inside setState.
Browse files Browse the repository at this point in the history
In a previous refactor, the setState methods were changed to use the prevState prop, but this lost the event received on the function, so the needed properties were desesctructured from the original event so it can be used inside the setState.

No breaking changes
  • Loading branch information
ernestognw committed Oct 9, 2019
1 parent 46d2de8 commit 60729c6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/components/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ export default class Carousel extends Component {
onMouseDown = (e, index) => {
e.preventDefault();
e.stopPropagation();
const { pageX } = e;
this.setState(() => ({
clicked: index,
dragStart: e.pageX,
dragStart: pageX,
}));
};

Expand All @@ -310,9 +311,10 @@ export default class Carousel extends Component {
* @param {event} e event
*/
onMouseMove = e => {
const { pageX } = e;
if (this.state.dragStart !== null) {
this.setState(previousState => ({
dragOffset: e.pageX - previousState.dragStart,
dragOffset: pageX - previousState.dragStart,
}));
}
};
Expand All @@ -323,9 +325,10 @@ export default class Carousel extends Component {
* @param {number} index of the element drag started on
*/
onTouchStart = (e, index) => {
const { changedTouches } = e;
this.setState(() => ({
clicked: index,
dragStart: e.changedTouches[0].pageX,
dragStart: changedTouches[0].pageX,
}));
};

Expand All @@ -338,9 +341,10 @@ export default class Carousel extends Component {
e.preventDefault();
e.stopPropagation();
}
const { changedTouches } = e;
if (this.state.dragStart !== null) {
this.setState(previousState => ({
dragOffset: e.changedTouches[0].pageX - previousState.dragStart,
dragOffset: changedTouches[0].pageX - previousState.dragStart,
}));
}
};
Expand Down

0 comments on commit 60729c6

Please sign in to comment.