diff --git a/package.json b/package.json index 18ce478..56802ef 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/VirtualList.js b/src/VirtualList.js index 6223c33..a2a74d5 100644 --- a/src/VirtualList.js +++ b/src/VirtualList.js @@ -18,6 +18,8 @@ const VirtualList = (options, mapVirtualToProps = defaultMapToVirtualProps) => ( itemBuffer: 0, }; + _isMounted = false; + constructor(props) { super(props); @@ -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); @@ -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);