2016-11-16 94 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) 
}