2016-11-08 112 views
1

我有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(),但沒有得到調用。

+0

您是否驗證了您的操作'this.props.fetchLiveRaceVideo'正被調用?您是否已經驗證過您期望的操作類型是由此操作發出的?另外,你是否檢查了reducer處理調度操作類型時返回的狀態? – Fizz

回答

1

可能不會調用componentWillReceiveProps,因爲會調用componentWillMount。我會放入一個console.log,看看是否是這種情況。

從文檔:

陣營安裝過程中不會調用初始道具componentWillReceiveProps。如果組件的一些道具可能更新,它只會調用這個方法。調用this.setState通常不會觸發componentWillReceiveProps。