Skip to content

Commit

Permalink
Updated Grid and Collection to move some state-setting logic related …
Browse files Browse the repository at this point in the history
…to offsets from componentWillUpdate to componentWillReceiveProps. This change should have no externally visible impact.
  • Loading branch information
Brian Vaughn committed Feb 20, 2017
1 parent 1135ad1 commit 38b3a40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
24 changes: 12 additions & 12 deletions source/Collection/CollectionView.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ export default class CollectionView extends PureComponent {
super(props, context)

this.state = {
calculateSizeAndPositionDataOnNextUpdate: false,
isScrolling: false,
scrollLeft: 0,
scrollTop: 0
}

this._calculateSizeAndPositionDataOnNextUpdate = false

// Invokes callbacks only when their values have changed.
this._onSectionRenderedMemoizer = createCallbackMemoizer()
this._onScrollMemoizer = createCallbackMemoizer(false)
Expand All @@ -163,9 +164,8 @@ export default class CollectionView extends PureComponent {
* Since cell positions are calculated by callbacks, the collection view has no way of detecting when the underlying data has changed.
*/
recomputeCellSizesAndPositions () {
this.setState({
calculateSizeAndPositionDataOnNextUpdate: true
})
this._calculateSizeAndPositionDataOnNextUpdate = true
this.forceUpdate()
}

/* ---------------------------- Component lifecycle methods ---------------------------- */
Expand Down Expand Up @@ -273,12 +273,14 @@ export default class CollectionView extends PureComponent {
* 2) New scroll props overriding the current state
* 3) Cells-count or cells-size has changed, making previous scroll offsets invalid
*/
componentWillUpdate (nextProps, nextState) {
componentWillReceiveProps (nextProps) {
const { scrollLeft, scrollTop } = this.state

if (
nextProps.cellCount === 0 &&
(
nextState.scrollLeft !== 0 ||
nextState.scrollTop !== 0
scrollLeft !== 0 ||
scrollTop !== 0
)
) {
this._setScrollPosition({
Expand All @@ -298,15 +300,13 @@ export default class CollectionView extends PureComponent {
if (
nextProps.cellCount !== this.props.cellCount ||
nextProps.cellLayoutManager !== this.props.cellLayoutManager ||
nextState.calculateSizeAndPositionDataOnNextUpdate
this._calculateSizeAndPositionDataOnNextUpdate
) {
nextProps.cellLayoutManager.calculateSizeAndPositionData()
}

if (nextState.calculateSizeAndPositionDataOnNextUpdate) {
this.setState({
calculateSizeAndPositionDataOnNextUpdate: false
})
if (this._calculateSizeAndPositionDataOnNextUpdate) {
this._calculateSizeAndPositionDataOnNextUpdate = false
}
}

Expand Down
14 changes: 9 additions & 5 deletions source/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,14 @@ export default class Grid extends PureComponent {
* 2) New scroll props overriding the current state
* 3) Cells-count or cells-size has changed, making previous scroll offsets invalid
*/
componentWillUpdate (nextProps, nextState) {
componentWillReceiveProps (nextProps) {
const { scrollLeft, scrollTop } = this.state

if (
nextProps.columnCount === 0 &&
nextState.scrollLeft !== 0 ||
scrollLeft !== 0 ||
nextProps.rowCount === 0 &&
nextState.scrollTop !== 0
scrollTop !== 0
) {
this._setScrollPosition({
scrollLeft: 0,
Expand Down Expand Up @@ -582,7 +584,7 @@ export default class Grid extends PureComponent {
nextCellSize: nextProps.columnWidth,
nextScrollToIndex: nextProps.scrollToColumn,
scrollToIndex: this.props.scrollToColumn,
updateScrollOffsetForScrollToIndex: () => this._updateScrollLeftForScrollToColumn(nextProps, nextState)
updateScrollOffsetForScrollToIndex: () => this._updateScrollLeftForScrollToColumn(nextProps, this.state)
})
calculateSizeAndPositionDataAndUpdateScrollOffset({
cellCount: this.props.rowCount,
Expand All @@ -593,9 +595,11 @@ export default class Grid extends PureComponent {
nextCellSize: nextProps.rowHeight,
nextScrollToIndex: nextProps.scrollToRow,
scrollToIndex: this.props.scrollToRow,
updateScrollOffsetForScrollToIndex: () => this._updateScrollTopForScrollToRow(nextProps, nextState)
updateScrollOffsetForScrollToIndex: () => this._updateScrollTopForScrollToRow(nextProps, this.state)
})
}

componentWillUpdate (nextProps, nextState) {
this._calculateChildrenToRender(nextProps, nextState)
}

Expand Down

0 comments on commit 38b3a40

Please sign in to comment.