2017-03-22 84 views
1

我有,我已经放在一个单独的文件,它看起来像这样一个模式(编辑为简洁起见):与子数据调用父功能反应本土

const DelegateModal = ({ selected, selectedFunc, visible, press}) => { 

return (
    <View style={{ marginTop: 50, marginBottom: 50 }}> 
     <Modal 
      animationType={'slide'} 
      visible={visible} 
      onRequestClose={() => { }} 
     > 
      <View style={{ marginTop: 60 }}> 
       <View> 
        <Text>Please select an expiry period for the delegate token</Text> 
        <Picker 
         selectedValue={selected} 
         onValueChange={pickerValue => { 
          selectedFunc(pickerValue); 
         } 
         } 

然后我从父与称它为:

renderDelegateDeliveryModal() { 
     if (this.state.delegateModalVisible === true) { 
      return (<DelegateModal 
       press={() => this.setDelegateModalVisible(false)} 
       visible={this.state.setDelegateModalVisible} 
       selected={this.state.delegatePicker} 
       selectedFunc={() => this.changePickerValue()} 
      />); 
     } 
    } 

随着changepickerValue()(父)是:

changePickerValue(pickerValue){ 
    console.log(pickerValue); 
} 

不管我做什么,但在C中的console.log hangePickerValue总是未定义的。如果我在onValueChange中放置一个console.log,pickerValue被设置,它只是没有被传递给父级。

任何建议,我哪里会出错?

谢谢

回答

1

您缺少阅读selectedFun回调中的pickerValue。您可以通过

renderDelegateDeliveryModal() { 
     if (this.state.delegateModalVisible === true) { 
      return (<DelegateModal 
       press={() => this.setDelegateModalVisible(false)} 
       visible={this.state.setDelegateModalVisible} 
       selected={this.state.delegatePicker} 
       selectedFunc={(pickerValue) => this.changePickerValue(pickerValue)} 
      />); 
     } 
    } 

OR

renderDelegateDeliveryModal() { 
     if (this.state.delegateModalVisible === true) { 
      return (<DelegateModal 
       press={() => this.setDelegateModalVisible(false)} 
       visible={this.state.setDelegateModalVisible} 
       selected={this.state.delegatePicker} 
       selectedFunc={this.changePickerValue} 
      />); 
     } 
    } 
+0

或者修复它唉唉就是这样。非常感谢 :) – Jingo