Skip to content

Commit a61bd5f

Browse files
optimizations: setState in lifecycles + forced reflow (#556)
1 parent 067fbdb commit a61bd5f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/Draggable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ class Draggable extends React.Component<DraggableProps, DraggableState> {
236236
}
237237

238238
componentWillUnmount() {
239-
this.setState({dragging: false}); // prevents invariant if unmounted while dragging
239+
if (this.state.dragging) {
240+
this.setState({dragging: false}); // prevents invariant if unmounted while dragging
241+
}
240242
}
241243

242244
// React Strict Mode compatibility: if `nodeRef` is passed, we will use it instead of trying to find

lib/DraggableCore.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,16 @@ export default class DraggableCore extends React.Component<DraggableCoreProps> {
265265
removeEvent(ownerDocument, eventsFor.mouse.stop, this.handleDragStop);
266266
removeEvent(ownerDocument, eventsFor.touch.stop, this.handleDragStop);
267267
removeEvent(thisNode, eventsFor.touch.start, this.onTouchStart, {passive: false});
268-
if (this.props.enableUserSelectHack) removeUserSelectStyles(ownerDocument);
268+
if (this.props.enableUserSelectHack) {
269+
// prevent a possible "forced reflow"
270+
if (window.requestAnimationFrame) {
271+
window.requestAnimationFrame(() => {
272+
removeUserSelectStyles(ownerDocument);
273+
});
274+
} else {
275+
removeUserSelectStyles(ownerDocument);
276+
}
277+
}
269278
}
270279
}
271280

0 commit comments

Comments
 (0)