Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

this.state.targetItems carries outdated values when items prop changes #129

Open
umarashfaq opened this issue Feb 10, 2020 · 0 comments
Open

Comments

@umarashfaq
Copy link

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)
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant