react-scrollspy icon indicating copy to clipboard operation
react-scrollspy copied to clipboard

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

Open umarashfaq opened this issue 5 years ago • 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)
}

umarashfaq avatar Feb 10 '20 23:02 umarashfaq