2016-11-16 96 views
0

獲取數據,並在組件的狀態陣營父組件的道具不從AJAX調用傳遞到子組件

_getDataFromServer(){ 
reqwest({ 
     url: 'http://127.0.0.1:8000/api/example/' 
    , type: 'json' 
    , method: 'get' 
    , contentType: 'application/json' 
    , crossOrigin: true 
    , withCredentials: false 
    , error: function (err) { } 
    , success: function (resp) { 
     const data = resp 
     this.setState({config:data}) 
     }.bind(this) 
    }) 
} 

componentWillMount(){ 

    this._getDataFromServer() 
} 

存儲和狀態傳遞JSON數據到子組件中的渲染方法

render() { 
    return (
     <Panel> 
      <AppDynamic uiConfig={this.state.config}/> 
     </Panel> 
    ); 
} 

但道具都在孩子component.i着空明白爲什麼道具不會傳遞給子組件

+0

控制檯中的任何錯誤? –

+1

請記住,網絡調用需要一些時間才能返回,所以在初始渲染時配置將不會處於組件狀態。 – ArneHugo

+0

問題已解決。在將參數傳遞給子組件後,我將它們存儲在狀態中。那個錯誤,我完成了 –

回答

0

嘗試上下文結合_getDataFromServer方法在構造函數中。

的Javascript

constructor(props){ 
    super(props); 
    this._getDataFromServer = this._getDataFromServer.bind(this) 
}