Last active
June 27, 2023 15:36
-
-
Save expressiveco/d0063875ab15631199acc27abf633177 to your computer and use it in GitHub Desktop.
Refreshing React Native WebView to initial url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
})); | |
} | |
} | |
} | |
/> | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good idea 👍