React Native 调用函数以在渲染中打开对话框

React Native calling a function to open a dialog in render

我在我的 React 本机应用程序中使用这个库 (https://github.com/jacklam718/react-native-popup-dialog/blob/master/README.md) 打开一个对话框。到目前为止,我遵循了他们提供的示例代码,它对我来说效果很好:

import PopupDialog from 'react-native-popup-dialog';

<View style={styles.container}>
  <Button
    text="Show Dialog"
    onPress={() => {
      this.popupDialog.show();
    }}
  />
  <PopupDialog
    ref={(popupDialog) => { this.popupDialog = popupDialog; }}
  >
    <View>
      <Text>Hello</Text>
    </View>
  </PopupDialog>
</View>

我遇到的问题是在我的屏幕首次加载时打开对话框。我尝试做类似的事情: {this.popupDialog.show()} 和 {() => this.popupDialog.show()} 以及其他无效的变体。我希望能够打开对话框 w/out,必须单击一个按钮才能调用 onPress() 函数。谁能指导我在正确的地方。

您或许可以使用组件生命周期来调用 popupDialog.show() 函数。

尝试将此添加到您的视图脚本中:

var self = this;
componentDidMount() {
   self.popupDialog.show();
}

https://facebook.github.io/react/docs/state-and-lifecycle.html

了解更多关于 React 中的状态和生命周期的信息

希望对您有所帮助。