RN項目中關於父子組件的通訊

子組件向父組件傳遞數據

子控件中在相應的函數中。經過props.coallback的回調通知父組件。函數

父組件調用callback屬性時行 綁定,並在方法中去解析使用獲取到的值 。this

//子控件:
<TouchableHighlight onPress={this._onPressButton.bind(this)}>
_onPressButton(){
this.props.callback("hello wrold");
}

//父控件
 <View style={{alignItems: 'center'}} callback={this._onPressButton.bind(this)}>
_onPressButton(str){
str....
}

父組件向子組件傳遞數據

經過ref屬性實現
父組件直接經過ref的屬性找到相應的子組件,調用相對應的方法。code

<AssetList  ref='myRef' dtList={this.state.data[this.state.index]}/>
  
  this.refs.myRef.setState(
            {
                data:this.state.data[this.state.index]
            }

        )
相關文章
相關標籤/搜索