Skip to content

Commit

Permalink
Fix NPE in StrictMode (#657)
Browse files Browse the repository at this point in the history
Tie up setting up and cleaning up listeners to lifecycle methods under the assumption that
lifecycle methods like `componentDidMount`/`componentWillUnmount` might be called multiple times
for the same instance.
  • Loading branch information
pradeepnschrodinger authored Nov 25, 2022
1 parent 3151457 commit f9e36cb
Showing 1 changed file with 62 additions and 37 deletions.
99 changes: 62 additions & 37 deletions src/FixedDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,43 +508,77 @@ class FixedDataTable extends React.Component {
this._didScrollStop = debounceCore(this._didScrollStopSync, 200, this);
this._onKeyDown = this._onKeyDown.bind(this);

this._wheelHandler = new ReactWheelHandler(
this._onScroll,
this._shouldHandleWheelX,
this._shouldHandleWheelY,
this.props.isRTL,
this.props.stopScrollDefaultHandling,
this.props.stopScrollPropagation
);

this._touchHandler = new ReactTouchHandler(
this._onScroll,
this._shouldHandleTouchX,
this._shouldHandleTouchY,
this.props.stopScrollDefaultHandling,
this.props.stopScrollPropagation
);
this._setupHandlers();
}

componentWillUnmount() {
this._cleanupHandlers();

// Cancel any pending debounced scroll handling and handle immediately.
this._didScrollStop.reset();
this._didScrollStopSync();
}

_setupHandlers() {
if (!this._wheelHandler) {
this._wheelHandler = new ReactWheelHandler(
this._onScroll,
this._shouldHandleWheelX,
this._shouldHandleWheelY,
this.props.isRTL,
this.props.stopScrollDefaultHandling,
this.props.stopScrollPropagation
);
}

if (!this._touchHandler) {
this._touchHandler = new ReactTouchHandler(
this._onScroll,
this._shouldHandleTouchX,
this._shouldHandleTouchY,
this.props.stopScrollDefaultHandling,
this.props.stopScrollPropagation
);
}

// TODO (pradeep): Remove these and pass to our table component directly after
// React provides an API where event handlers can be specified to be non-passive (facebook/react#6436)
this._divRef &&
this._divRef.removeEventListener('wheel', this._wheelHandler.onWheel, {
if (this._divRef) {
this._divRef.addEventListener('wheel', this._wheelHandler.onWheel, {
passive: false,
});
this._divRef &&
this._divRef.removeEventListener(
}
if (this.props.touchScrollEnabled && this._divRef) {
this._divRef.addEventListener(
'touchmove',
this._touchHandler.onTouchMove,
{ passive: false }
);
this._wheelHandler = null;
this._touchHandler = null;
}
}

// Cancel any pending debounced scroll handling and handle immediately.
this._didScrollStop.reset();
this._didScrollStopSync();
_cleanupHandlers() {
if (this._wheelHandler) {
if (this._divRef) {
this._divRef.removeEventListener('wheel', this._wheelHandler.onWheel, {
passive: false,
});
}
this._wheelHandler = null;
}

if (this._touchHandler) {
if (this._divRef) {
this._divRef.removeEventListener(
'touchmove',
this._touchHandler.onTouchMove,
{
passive: false,
}
);
}
this._touchHandler = null;
}
}

_shouldHandleTouchX = (/*number*/ delta) /*boolean*/ =>
Expand Down Expand Up @@ -646,18 +680,7 @@ class FixedDataTable extends React.Component {
}

componentDidMount() {
this._divRef &&
this._divRef.addEventListener('wheel', this._wheelHandler.onWheel, {
passive: false,
});
if (this.props.touchScrollEnabled) {
this._divRef &&
this._divRef.addEventListener(
'touchmove',
this._touchHandler.onTouchMove,
{ passive: false }
);
}
this._setupHandlers();
this._reportContentHeight();
this._reportScrollBarsUpdates();
}
Expand Down Expand Up @@ -1037,6 +1060,8 @@ class FixedDataTable extends React.Component {
this._divRef = div;
if (this.props.stopReactWheelPropagation) {
this._wheelHandler.setRoot(div);
} else {
this._wheelHandler.setRoot(null);
}
};

Expand Down

0 comments on commit f9e36cb

Please sign in to comment.