【问题标题】:React parent component props are not passing to child component反应父组件道具没有传递给子组件
【发布时间】: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


【解决方案1】:

尝试将上下文绑定到构造函数内的_getDataFromServer 方法。

Javascript

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

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2021-08-14
    • 2022-06-30
    • 2020-09-01
    相关资源
    最近更新 更多