Skip to content

Instantly share code, notes, and snippets.

@expressiveco
Last active June 27, 2023 15:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expressiveco/d0063875ab15631199acc27abf633177 to your computer and use it in GitHub Desktop.
Save expressiveco/d0063875ab15631199acc27abf633177 to your computer and use it in GitHub Desktop.
Refreshing React Native WebView to initial url
export default class MyWeb extends Component {
mainUrl = "https://facebook.github.io/";
state = {
url: this.mainUrl,
};
render() {
return [
<WebView
key="comp1"
ref={
ref => {
this.webView.ref = ref;
}
}
source={
{
uri: this.state.url
}
}
onLoad={
e => {
// Update the state so url changes could be detected by React and we could load the mainUrl.
this.state.url = e.nativeEvent.url;
}
}
/>,
<Button
key="comp2"
onPress={
() => {
if (this.state.url == this.mainUrl) {
this.webView.ref.reload();
} else {
this.setState(prevState => ({
url: this.mainUrl
}));
}
}
}
/>
];
}
}
@gre-dev
Copy link

gre-dev commented Jul 6, 2021

Good idea 👍

@gssj85
Copy link

gssj85 commented Dec 14, 2021

Thank you

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