【发布时间】:2016-11-16 12:00:43
【问题描述】:
从 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>
);
}
但是子组件中的道具是空的。我不明白为什么道具没有传递给子组件
【问题讨论】:
-
控制台有错误吗?
-
请记住,网络调用需要一些时间才能返回,因此在初始渲染时,配置不会处于组件状态。
-
问题已解决。将参数传递给子组件后,我将它们存储在状态中。那个错误,我做了
标签: javascript reactjs reactive-programming