I am trying to implement react native webview component in my application, but the web view is not loading any url its just showing the white page.
var React
<View>
<WebView
source={{uri: this.props.link}}
style={styles.webview}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
/>
</View>
and style as follows:
const React = require('react-native');
const { Dimensions } = React;
const deviceHeight = Dimensions.get('window').height;
const deviceWidth = Dimensions.get('window').width;
export default {
webview: {
width: deviceWidth,
height: deviceHeight
}
};
All this to deal with bad webview dimension, so just set a specific height and specific width too (deviceHeight and deviceWidth as the example above).
I ran into the same issue recently. And I found that
alignment: 'center'
was causing the issue for me. I commented it and the webView got loaded immediately. I found the solution here : https://github.com/facebook/react-native/issues/5974 'brunocvcunha's' response worked for me.
As of June 2020 (noting the date because React Native answers seem to become out-of-date quickly), the simplest solution to this appears to be:
import React from 'react'
import { View, StyleSheet } from 'react-native'
import { WebView } from 'react-native-webview'
export const ComponentWithWebView = () => {
return (
<View style={styles.view}>
<WebView source = {{uri: 'https://www.google.com/'}} />
</View>
)
}
const styles = StyleSheet.create({
view: {
alignSelf: 'stretch',
flex: 1,
}
}
This results in a WebView
filling the available space and being nested within a View
. I believe the typical problems faced when placing a WebView
within a View
is that View
expects children to force the View
to expand (that is, a Text
component would take up some amount of width and height which the View
then accommodates). WebView
, on the other hand, expands to the size of the parent component unless a style is passed specifying the width
. Therefore, a simple <View><WebView /></View>
results in a 0 width and nothing shown on the screen. The earlier solutions of setting the WebView
width work well but require either the device dimensions to be fetched (which might not be the desired width) or for the View
to have an onLayout
function AND have some way to expand the View
to the desired space. I found it easiest to just apply the flex: 1
and alignSelf: 'stretch'
for the View
to fill the space as desired and then WebView
to automatically follow suit.
Hope this helps someone before it becomes obsolete!
If you want the component to render the entire page, you need to wrap it with View that has flex: 1
. The code below works for me:
<View style={{flex:1, alignItems: 'flex-end'}}>
<WebView
source={{uri: this.state.webContentLink}}
startInLoadingState={true}
scalesPageToFit={true} />
</View>
import { WebView } from 'react-native'; is deprecated
use below line instead
npm install react-native-render-html@4.1.2 --save
then
import HTML from 'react-native-render-html';
react-native-render-html starting with version 4.2.0, react-native-webview is now a peer dependency. As a result, you need to install it yourself.
I had this issue. WebView would render when it was the only component returned, but not when nested in another View component.
For reasons I'm not entirely sure of the issue was resolved by setting a width property on the WebView component.
class App extends React.Component {
render() {
return (
<View style={styles.container}>
<WebView
source={{uri: 'https://www.youtube.com/embed/MhkGQAoc7bc'}}
style={styles.video}
/>
<WebView
source={{uri: 'https://www.youtube.com/embed/PGUMRVowdv8'}}
style={styles.video}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'space-between',
},
video: {
marginTop: 20,
maxHeight: 200,
width: 320,
flex: 1
}
});