Skip to content

Instantly share code, notes, and snippets.

@JuReyms
Forked from sdiama/webview.js
Last active February 1, 2021 19:31
Show Gist options
  • Save JuReyms/313ea955b5876d079845b74d6681553f to your computer and use it in GitHub Desktop.
Save JuReyms/313ea955b5876d079845b74d6681553f to your computer and use it in GitHub Desktop.
React Native: Handle hardware back button @ webview
import * as React from 'react';
import { BackHandler } from 'react-native';
import { WebView } from 'react-native-webview';
export default class App extends React.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://www.mywebsite.xyz' }}
style={{ marginTop: 15}}
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