Skip to content

Instantly share code, notes, and snippets.

@sdiama
Created May 29, 2019 17:47
Show Gist options
  • Select an option

  • Save sdiama/0b8cd60e63ea581cd1abdeea348dbab2 to your computer and use it in GitHub Desktop.

Select an option

Save sdiama/0b8cd60e63ea581cd1abdeea348dbab2 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)}
/>
)
}
}
@barrosohub

Copy link
Copy Markdown

Thank You!

It helped me. I would definitely be able to resolve it. But you made me optimize my time. How do I buy you coffee?

@Paydarsefat

Copy link
Copy Markdown

thank you sir

@natanaeldiego

Copy link
Copy Markdown

Thank You!

@kartikkapoor12

Copy link
Copy Markdown

thankyou sir

@mangowi

mangowi commented May 23, 2020

Copy link
Copy Markdown

Thank you so much @sdiama.

Anyone has an idea on how to handle a button click from the WebView URL loaded to the local native app.
While passing some values from the data attributes. Thank you in advance guys

@kartikkapoor12 @natanaeldiego @barrsofilho, @Paydarsefat

@licguzman

Copy link
Copy Markdown

expo tells me this when i try the code
Device: (108:85823) Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
what can i do?

@JuReyms

JuReyms commented Jun 19, 2020

Copy link
Copy Markdown

Perfect ! thanks

i use : import { WebView } from 'react-native-webview';

@blkc

blkc commented Sep 24, 2020

Copy link
Copy Markdown

Thank you so much @sdiama.

Anyone has an idea on how to handle a button click from the WebView URL loaded to the local native app.
While passing some values from the data attributes. Thank you in advance guys

@kartikkapoor12 @natanaeldiego @barrsofilho, @Paydarsefat

Thanks for the gist 😊. @mangowi Checkout https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md on onNavigationStateChange

@mangowi

mangowi commented Oct 23, 2020

Copy link
Copy Markdown

Thanks @blkc

@pOlimgit

Copy link
Copy Markdown

Thank you so much @sdiama

@shubhamexpo

shubhamexpo commented Mar 18, 2021

Copy link
Copy Markdown

after implementation, it's working fine, but the react-navigation is getting crash.

@mgamboa-pernix

Copy link
Copy Markdown

Thank you!!

@lviv-dev

lviv-dev commented May 6, 2021

Copy link
Copy Markdown

Awesome! Thanks you so much!

@sfarkas1988

Copy link
Copy Markdown

It works perfect on android but on ios no back gesture is detected for me on ios 14.5 iphone 12 pro max simulator as well as on my physical ipad and a friends iphone 12.

@sfarkas1988

Copy link
Copy Markdown

allowsBackForwardNavigationGestures={true}
Thats it for ios.

@mvphaum

mvphaum commented Sep 17, 2021

Copy link
Copy Markdown

Sorry, i'm a newbie in Webview React Native App developing. Want to ask, how do i implement this code in my responsive app that run by PHP.

@mvphaum

mvphaum commented Sep 17, 2021

Copy link
Copy Markdown

perhaps you could provide some help here?

@mvphaum

mvphaum commented Sep 17, 2021

Copy link
Copy Markdown

Thank You!

It helped me. I would definitely be able to resolve it. But you made me optimize my time. How do I buy you coffee?

can help explain how it works?

@jittusaroj

Copy link
Copy Markdown

thankyou so much sir i was in trouble for a long time for implementing back button feature in hybrid app
thankyou so much

@movebx

movebx commented May 18, 2023

Copy link
Copy Markdown

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment