呼叫componentWillReceiveProps反应,和/终极版
问题描述:
我有2个组件呼叫componentWillReceiveProps反应,和/终极版
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)动作。 我从这个ajax调用中获得“链接”。并且该链接正在更新状态。
状态更新后,我想调用componentWillReceiveProps(),但没有得到调用。
答
可能不会调用componentWillReceiveProps,因为会调用componentWillMount。我会放入一个console.log,看看是否是这种情况。
从文档:
阵营安装过程中不会调用初始道具componentWillReceiveProps。如果组件的一些道具可能更新,它只会调用这个方法。调用this.setState通常不会触发componentWillReceiveProps。
您是否验证了您的操作'this.props.fetchLiveRaceVideo'正被调用?您是否已经验证过您期望的操作类型是由此操作发出的?另外,你是否检查了reducer处理调度操作类型时返回的状态? – Fizz