Skip to content

Instantly share code, notes, and snippets.

@barrosohub
Forked from sdiama/webview.js
Created April 27, 2020 02:44
Show Gist options
  • Save barrosohub/10c6c5d9e4cf3512f201f63207106fb1 to your computer and use it in GitHub Desktop.
Save barrosohub/10c6c5d9e4cf3512f201f63207106fb1 to your computer and use it in GitHub Desktop.
React Native: Handle hardware back button @ webview
import React, { Component } from 'react';
import { WebView, BackHandler } from 'react-native';
export default class WebViewMoviezSpace extends Component {
constructor(props) {
super(props);
this.WEBVIEW_REF = React.createRef();
}
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
}
handleBackButton = ()=>{
this.WEBVIEW_REF.current.goBack();
return true;
}
onNavigationStateChange(navState) {
this.setState({
canGoBack: navState.canGoBack
});
}
render(){
return (
<WebView
source={{ uri: "https://moviez.space" }}
ref={this.WEBVIEW_REF}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
/>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment