在从reducer React / Redux更新状态后调用componentWillReceiveProps()

时间:2016-11-08 11:57:55

标签: reactjs redux react-redux

我有2个组件

1]家长 2]孩子

我将父组件操作传递给子级,以便在更改下拉列表时调用。

父组件方法是调用存储的函数(ajax调用)和更新状态变量。 更新状态后,我想在componentWillReceiveProps()中执行一些操作,但它不在componentWillReceiveProps()中。

以下是父组件中的代码 -

1]家长

   componentWillReceiveProps(props) {
    this._setTrackToParams(props)
    debugger;
    let liveVideoList = this.state.liveVideoList
    if(props.liveRaceVideos != null && _.isEmpty(this.state.liveVideoList)) {
        console.log('live race video if')
        this.state.liveVideoList.push(props.liveRaceVideos)
        this.setState(this.state)
    } else if(props.liveRaceVideos != null && this.selectedKey) {
        console.log('live race video else')
        this.state.liveVideoList[this.selectedKey] = props.liveRaceVideos
        this.setState(this.state)
    }
    console.log("bye")
}

  renderChild() {
    let selectValue = !this.selectedKey ? this.props.params.trackCode : this.selectedKey
    if(this.state.liveVideoList) {
        return _.map(this.state.liveVideoList,(value , key) => {
             if(key < 3) {
                return (
                    <LiveVideoPanel ref = "liveVideoPanel" key = {key} currentTracks = {this.props.currentTracks} liveVideos = {value} selectedValue = {selectValue} onChange = {this._toggleVideos} onChangeTrackList = {this.onChangeTrackList.bind(this)} videoCount = {key}/>
                )
             }
        })
    }
}


    onChangeTrackList(id, key) {
    if(id) {
        this.selectKey = key
        this.props.fetchLiveRaceVideo(id)
    }
}

所以我在更改下拉列表时调用this.onChangeTrackList()函数。 并且此函数在内部调用this.props.fetchLiveRaceVideo(id)动作。 我正在&#34;链接&#34;从这个ajax电话。并且该链接正在状态中更新。

状态更新后,我想调用componentWillReceiveProps(),但这不会被调用。

1 个答案:

答案 0 :(得分:1)

可能没有调用componentWillReceiveProps,因为正在调用componentWillMount。我会放入一个console.log,看看是否是这种情况。

来自文档:

  

在安装过程中,React不会使用初始道具调用componentWillReceiveProps。如果某些组件的道具可能会更新,它只会调用此方法。调用this.setState通常不会触发componentWillReceiveProps。

相关问题