makotot/react-scrollspy

`this.state.targetItems` carries outdated values when `items` prop changes

umarashfaq opened this issue · 0 comments

Root cause:
When componentWillReceiveProps is invoked, this.props still carries previous version of props. New version of props is passed as a parameter to componentsWillReceiveProps.

Current implementation:

_initFromProps () {
    const targetItems = this._initSpyTarget(this.props.items)

    this.setState({
      targetItems,
    })

    this._spy(targetItems)
}

UNSAFE_componentWillReceiveProps () {
    this._initFromProps()
}

Proposed implementation:

_initFromProps (_props) {
    const props = _props ? _props : this.props;
    const targetItems = this._initSpyTarget(props.items)

    this.setState({
      targetItems,
    })

    this._spy(targetItems)
}

UNSAFE_componentWillReceiveProps (nextProps) {
    this._initFromProps(nextProps)
}