React父组件props不会传递给子组件

时间:2016-11-16 12:00:43

标签: javascript reactjs reactive-programming

从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方法中的子组件

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

但是子组件中的道具是空的。我无法理解为什么道具没有传递给子组件

1 个答案:

答案 0 :(得分:0)

尝试将上下文绑定到构造函数内的constructor(props){ super(props); this._getDataFromServer = this._getDataFromServer.bind(this) } 方法。

<强>的Javascript

        <TextInput 
          style={styles.textInput}
          keyboardType = 'numeric'
          onChangeText = {(text)=> this.onChanged(text)}
          value = {this.state.myNumber}
        /> 

        onTextChanged(text) {
          // code to remove non-numeric characters from text
          this.setState({myNumber: text})
        }
相关问题