2015-10-01 40 views
0

我有一個反應路由器應用程序,它在/users上安裝User。它工作正常。當組件收到道具時如何做某事

然後,我點擊一個/users?page=2鏈接,通過新的道具。這裏getData使用this.props.location.query。因此,從componentWillReceiveProps調用getData將從第1頁而不是第2頁獲取內容。

如果它存在,我將使用組件HasReceived Props方法。我能做些什麼?

componentWillMount: function(){ 
    this.setState({data:null}); 
    this.getData(); 
    }, 

    componentWillReceiveProps: function(nextProps){ 
    this.setState({data:null}); 
    this.getData(); 
    }, 

回答

1

夫婦的事情。

1)使用this.setState內部componentWillMount工作,但沒有任何用途,因爲你可能只是在getInitialState內部完成此操作。 2)如果你想對這樣的道具變化作出反應,你需要使用componentWillreceiveProps裏面的nextProps對象。一個簡單的解決方法是更改​​您的getData方法以允許將nextProps傳遞給它。如:

getData: function(nextProps) { 
    var props = nextProps || this.props; 

    // your getData stuff 
} 

然後通過它在你的componentWillReceiveProps方法this.getData(nextProps)

相關問題