Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a React-Native Webview?

I'm new to React Native and I have a simple app which opens a Webview when a button is pressed. If the navigation state changes, I want to close the Webview. I'm able to know when to close it but unable to find how.

The doc does not mention any function to do it. What's the solution for this?

version : react-native: 0.47.2

like image 502
timp Avatar asked Sep 12 '17 09:09

timp


People also ask

How do I turn off Webview in react native?

Use onShouldStartLoadWithRequest prop of react-native-webview package. p.s. Return true from the function to continue loading the request and false to stop loading.

How do I close Webview?

Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.

How Webview works in react native?

WebViews offer developers opportunities to render any web components in a React Native application. A web component can be anything from a whole webpage/application or just a simple HTML file. The package react-native-webview makes it super simple to embed WebViews into your React Native apps!


1 Answers

you can add it in 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 >

 )
}
like image 110
Mohamed Khalil Avatar answered Oct 14 '22 03:10

Mohamed Khalil