如何关闭React-Native Webview?

tim*_*imp 6 javascript android android-webview react-native

我是React Native的新手,我有一个简单的应用程序,当按下按钮时会打开Webview.如果导航状态发生变化,我想关闭Webview.我能够知道何时关闭它但无法找到方法.

该文档没有提到任何功能.这是什么解决方案?

版本: react-native:0.47.2

Moh*_*lil 5

你可以在 Modal 中添加它

_onNavigationStateChange (webViewState) {
   this.hide()
}
show () {
  this.setState({ modalVisible: true })
}

hide () {
  this.setState({ modalVisible: false })
}

render () {
const { clientId, redirectUrl, scopes } = this.props
return (
  <Modal
    animationType={'slide'}
    visible={this.state.modalVisible}
    onRequestClose={this.hide.bind(this)}
    transparent
  >
    <View style={styles.modalWarp}>
      <View style={styles.contentWarp}>
        <WebView
          style={[{ flex: 1 }, this.props.styles]}
          source={{ uri: `http://google.com` }}
          scalesPageToFit
          startInLoadingState
          onNavigationStateChange={this._onNavigationStateChange.bind(this)}
          onError={this._onNavigationStateChange.bind(this)}
        />
        <TouchableOpacity onPress={this.hide.bind(this)} style={styles.btnStyle}>
          <Text style={styles.closeStyle}>close</Text>
        </TouchableOpacity>
      </View>
    </View>
  </Modal >

 )
}
Run Code Online (Sandbox Code Playgroud)