I am trying to use WebView Component inside View component, for a react native application I am working on. When I embed WebView inside View, I am not seeing the content I am displaying in WebView. Is this the expected behavior with react native?
-
5Normally the WebView should appear. Try setting flex to 1 for both the View and the WebView or setting explicit sizes.Nico– Nico2016-08-16 00:49:59 +00:00Commented Aug 16, 2016 at 0:49
-
could you place your sample code at rnplay.org?stereodenis– stereodenis2016-08-16 07:33:13 +00:00Commented Aug 16, 2016 at 7:33
-
3It should work. Set all your parent Views and your Webview to flex:1abeikverdi– abeikverdi2016-08-16 08:20:58 +00:00Commented Aug 16, 2016 at 8:20
-
Thank you all! I tried the flex: 1 approach. It worked!pg238– pg2382016-08-16 15:36:27 +00:00Commented Aug 16, 2016 at 15:36
-
Perhaps Nico or @abeikverdi should post your comments as answers, so user4122421 can accept it.Renato Back– Renato Back2016-09-07 02:57:00 +00:00Commented Sep 7, 2016 at 2:57
6 Answers
When it's nested inside a View component, you need to set both view and webview flex to 1.
eg. -
<View style={{flex:1}}>
<WebView
source={{ uri: url }}
style={{flex:1}}
/>
</View>
4 Comments
alignItems attribute which as I understood doesn't fit with WebViews. When I switched to a simple style just with flex: 1 the WebView appeared.Please use following code:
import React from 'react';
import {View, ImageBackground, StyleSheet} from 'react-native';
import { WebView } from 'react-native-webview';
export default class WebViewScreen extends React.Component {
render(){
return(
<View style={styles.container}>
<WebView
source= {{uri: 'https://www.google.com'}}
style= {styles.loginWebView}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'stretch',
},
loginWebView: {
flex: 1,
marginTop: 30,
marginBottom: 20
}
});
this code is working absolutely fine for me,
In all above solutions I observed that everyone is suggesting to add flex: 1 to your webView style.
Yes it is correct but in case you want to nest WebView inside View then you need to specify styles of your parent view precisely.
So, I set justifyContent: 'flex-start' so that vertically WebView will start from top of my screen and alignItems: 'stretch' so that WebView will take all available in horizontal direction
As we use justifyContent to specify primary axis alignment and alignItems to specify secondary axis alignment and default flexDirection is column.
To get more information on how to install react-native-webview please refer following link: https://github.com/react-native-community/react-native-webview/blob/master/docs/Getting-Started.md
1 Comment
For WebView in View, We can use this code for .js and .tsx files in React-native.
import React, { Component } from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';
export default class WebViewExample extends Component {
render() {
return (
<View style={{height:"100%" , width:"100%"}}>
<WebView source={{ uri: 'https://facebook.github.io/react-native/' }}/>
</View>
);
}
}
Comments
Try this to open a pdf file. Li'l out of the box but I am sure it would help someone :)
{
Platform.OS === "android" ?
<WebView
source={{uri:'http://docs.google.com/gview?embedded=true&url=https://your PDF Link'}}
style={{flex: 1}}
/>
:
<WebView
source={{uri:'https:your PDF Link'}}
style={{flex: 1}}
/>
}