如何创建 CallBack 方法以从另一个 JS 获取值?

IT技术 reactjs react-native react-native-android react-native-ios
2021-05-02 07:25:41

我已经CustomDatePicker.JS为 datePicker创建了文件,Android 使用DatePickerAndroid和 ios 使用DatePickerIOS,以及它在我的另一个Profile.js文件中的使用。我使用如下。

<CustomDatePicker ref='modal'></CustomDatePicker>

现在无论何时选择日期,CustomDatePicker.JS类都会获取值,但我想要Profile.js. 那么如何在配置文件中获取日期值。

CustomDatePicker.JS: Here 将获得日期和时间日志。 Profile.js: 想要在 CustomDatePicker 上选择的日期。

我希望,你明白...

谢谢。

1个回答

在您的Profile.js,即Profile component您可以传递一些回调 fn 作为props,例如,

<CustomDatePicker 
  mode="date"
  onDateChange={date => this.onDateChange(date)}
  ref='modal'>
</CustomDatePicker>

在你CustomDatePicker.js,即CustomDatePicker component你定义的地方DatePickerIOS,你可以使用这些props。

<DatePickerIOS
   {...this.props}
/>