Skip to content

Commit

Permalink
Merge pull request #66 from Ramshackle-Jamathon/master
Browse files Browse the repository at this point in the history
prevent trying to setState on unmounted VirtualList + checking for undefined state before comparison
  • Loading branch information
developerdizzle committed Dec 28, 2017
2 parents 90fec40 + 49940a7 commit 42dbd7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-virtual-list",
"version": "2.2.2",
"version": "2.2.3",
"description": "Super simple virtualized list React higher-order component",
"main": "lib/VirtualList.js",
"directories": {
Expand Down
16 changes: 15 additions & 1 deletion src/VirtualList.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const VirtualList = (options, mapVirtualToProps = defaultMapToVirtualProps) => (
itemBuffer: 0,
};

_isMounted = false;

constructor(props) {
super(props);

Expand Down Expand Up @@ -50,20 +52,30 @@ const VirtualList = (options, mapVirtualToProps = defaultMapToVirtualProps) => (
setStateIfNeeded(list, container, items, itemHeight, itemBuffer) {
// get first and lastItemIndex
const state = getVisibleItemBounds(list, container, items, itemHeight, itemBuffer);

if (state === undefined) { return; }

if (state.firstItemIndex > state.lastItemIndex) { return; }

if (state !== undefined && (state.firstItemIndex !== this.state.firstItemIndex || state.lastItemIndex !== this.state.lastItemIndex)) {
if (state.firstItemIndex !== this.state.firstItemIndex || state.lastItemIndex !== this.state.lastItemIndex) {
this.setState(state);
}
}

refreshState() {
if (!this._isMounted) {
return;
}

const { itemHeight, items, itemBuffer } = this.props;

this.setStateIfNeeded(this.domNode, this.options.container, items, itemHeight, itemBuffer);
};

componentWillMount() {
this._isMounted = true;
}

componentDidMount() {
// cache the DOM node
this.domNode = ReactDOM.findDOMNode(this);
Expand All @@ -77,6 +89,8 @@ const VirtualList = (options, mapVirtualToProps = defaultMapToVirtualProps) => (
};

componentWillUnmount() {
this._isMounted = false;

// remove events
this.options.container.removeEventListener('scroll', this.refreshState);
this.options.container.removeEventListener('resize', this.refreshState);
Expand Down

0 comments on commit 42dbd7d

Please sign in to comment.