Last active
April 2, 2020 08:24
-
-
Save tylerthebuildor/9e50dcec14362349c392 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { | |
Linking, | |
StyleSheet, | |
WebView, | |
} from 'react-native'; | |
import SomeHTMLFile from './some-html-file.html | |
export default class WebViewExt extends React.Component { | |
constructor() { | |
super(); | |
} | |
render() { | |
return( | |
<WebView | |
source={SomeHTMLFile} | |
onShouldStartLoadWithRequest={this.openExternalLink} | |
style={styles.webview} /> | |
) | |
} | |
openExternalLink(req) { | |
const isLocal = req.url.search('http://localhost') !== -1; | |
if (isLocal) { | |
return true; | |
} else { | |
Linking.openURL(req.url); | |
return false; | |
} | |
} | |
} | |
const styles = StyleSheet.create({ | |
webview: { | |
flex: 1, | |
alignItems: 'stretch', | |
}, | |
}); |
This code only working in iOS platform
I can't believe there isn't something in vanilla RN for this. Expo's component is perfect. I wish they would submit a pull request to the main RN branch.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tylerbuchea so does this force WebView links, once pressed, to be opened within an actual browser?