59

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 = require('react-native');
var{
 View,
 Text,
 StyleSheet,
 WebView
} = React;


module.exports = React.createClass({
 render: function(){
   return(
     <View style={styles.container}>
      <WebView source={{uri: 'https://m.facebook.com'}} style= {styles.webView}/>
     </View>
   );
 }
});

var styles = StyleSheet.create({
   container: {
     flex:1,
     backgroundColor:  '#ff00ff'
   },webView :{
     height: 320,
     width : 200
   }
});

Below is the screenshot of the output . image

1
  • 2
    just a note that webView includes 2 styles. one for container and one standard one. style={{ // add your style, eg. backgroundColor: 'transparent' }} containerStyle={[{ //add your style, eg. flex: 0, width: 300, height: 300 }]}
    – verunar
    Commented Jan 13, 2021 at 16:55

19 Answers 19

75

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
  }
});
5
  • 8
    setting flex: 1 didn't work for me, on Android at least. Explicitly setting height and width did, however. (nested inside a <View />)
    – agm1984
    Commented Oct 16, 2017 at 0:22
  • 3
    Seems like it's quite fickle - for me, flex: 1 worked once I removed justify/align props. Guess I'll work my way from there now.
    – AndyO
    Commented Jun 1, 2019 at 18:23
  • hi @AndyO, how did you manage to make the video 100% width in this case to make it responsive?
    – verunar
    Commented Dec 7, 2020 at 12:54
  • @verunar I haven't used react native in many months. But what I can see in the code is that a) I'm not using the built-in web view but react-native-webview b) I have only flex: 1 on the parent and no styling on the web view itself.
    – AndyO
    Commented Dec 8, 2020 at 18:20
  • for me, it was very weird solution, I just removing the URL and pasting it again a couple of times to make the app reload, and then it showing. I'm doing this after any change to the website because I noticed this reloads the web view cache
    – Anas
    Commented Feb 18, 2024 at 17:51
40

I'm facing same issue. What I observed is that WebView doesn't work if it's nested. If component returns just WebView, then everything is fine.

2
  • 4
    This should be the correct answer. Specifying width for the webview is an obnoxious idea, especially when you need to cater to a wide range of devices with varying width.
    – Deepak G M
    Commented Dec 7, 2018 at 8:53
  • Isn't WebView a component that just returns a WebView?
    – Sandy
    Commented Sep 19, 2019 at 12:30
13

Using the answers from other users, I was able to get my react native with webview working both inside a view and outside a view. My problem came down to two things. Being on the android emulator and behind a proxy, I just had to go to my browser (chrome) in the android emulator and sign in to the corporate proxy. Secondly, some sites work and others will not work. Whether the webview was nested or not inside of a View tag, some sites like cnn.com and slack.com etc will work fine, but no matter what settings I tried for google.com it wouldn't work (even though the proxy will definitely allow google.com) Lastly, when I rebuild my application and push to the emulator the new app, sometimes it took an inordinately long time to load any site. But once the site was loaded, the links are quick and responsive. So if you don't at first see something after a build, also be patient. Hope this helps someone else.

My final app.js

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View,
  Dimensions
} from 'react-native';

import { WebView } from 'react-native';

const deviceHeight = Dimensions.get('window').height;
const deviceWidth = Dimensions.get('window').width;

type Props = {};
export default class App extends Component<Props> {

  render() {
    return (
<View style={{flex:1}}>
  <WebView
   style={styles.webview}
   source={{uri: 'https://www.slack.com'}}
   javaScriptEnabled={true}
   domStorageEnabled={true}
   startInLoadingState={false}
   scalesPageToFit={true} />
</View>
    );
  }
}

const styles = StyleSheet.create({
  webview: {
    flex: 1,
    backgroundColor: 'yellow',
    width: deviceWidth,
    height: deviceHeight
  }
});
1
  • No need to set flex: 1, you already cover the deviceWidth and deviceHeight. Commented Feb 5, 2019 at 9:26
10

WebView works well on Android. However you need to enable javascript and dom storage for some web pages.

<WebView style={styles.webView}
    source={{uri: 'https://google.com/'}}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    startInLoadingState={true}
    >
</WebView>
0
9

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>
9

WebView is being moved to react-native-webview .

None of the other answers worked except this method:

npm install --save react-native-webview

Then use it as follows:

<View style={{ flex: 1, alignItems: 'flex-end' }}>
      <WebView
        source={{
          uri: 'https://www.yahoo.com',
        }}
        startInLoadingState={true}
        scalesPageToFit={true}
        style={{
          width: 320,
          height: 300,
        }}
      />
</View>
0
7
<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).

6

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!

2

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.

1

Let me give the simplest example which will work seamlessly:

import React from 'react';
import { WebView  } from 'react-native';

export default class App extends React.Component {
  render() {
    return (
      <WebView
      source={{uri: 'https://github.com/facebook/react-native'}} 
    />

    );
  }
}

Do not add your WebView component within a view that created problem and webview url is not rendered rather styles of view will be shown.

0

I had the same issue and spent a day attempting to fix it. I copied in the UIExplorer webview example, and that didn't work.

I ultimately ended up upgrading react and creating a new react-native project and copying the files in there, and that fixed it.

I wish I had a better answer as to why that fixed it, but hopefully that helps

0

I am doing React Native Webview, Could you please suggest me how to makeWebview loading the uri

render() {
        return (
            <Modal
                animationType="slide"
                ref={"webModal"}
                style={{
                    justifyContent: 'center',
                    borderRadius: Platform.OS === 'ios' ? 30 : 0,
                    width: screen.width,
                    height: screen.height,borderColor:'red',
                    borderWidth: 5
                }}
                position='center'
                backdrop={false}
                onClosed={() => {
                    // alert("Modal closed");
                }}>

                <View style={{ flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: 20, top: 10 }} >
                    <Text style={{ fontSize: 24, fontWeight: '700' }}>
                        Interests
                                 </Text>
                    <Icon name="ios-close" size={40} color='purple' onPress={() => { this.refs.webModal.close() }} />
                </View>
                <WebView
                    source={{ uri: this.state.link }}
                    style={{ marginTop: 20,borderColor:'green',
                    borderWidth: 5 }}
                />

            </Modal>
        );
    }
}
1
0

Try

<WebView
  source={{ uri: "https://inhall.in/" }}
  style={Styles.webView}
  javaScriptEnabled={true}
  scalesPageToFit />

javaScriptEnabled={true} might help

0

The only problem here is the width. Width in percentage will not work and flex will also not work. So set the width in numbers or you can get the device width and set that. that's it.

import { Dimensions } from 'react-native';
const deviceWidth = Dimensions.get('window').width;

and then in WevView

<View style={{ width: '100%', height: '100%' }}>
    <WebView
        mixedContentMode="compatibility"
        source={{ uri: 'https://www.google.com' }}
        startInLoadingState={true}
        scalesPageToFit={true}
        style={{
            width: deviceWidth,
              height: "100%",
            }}
    />
</View>
0

None of the other answers worked for me. I figured it was a compatibility issue with react-native navigation for me. i just didnt get it to work with other markup inside a tab navigation. moving it into a stack navigation fixed it for me, no idea how to make it work with tabs.

0

if by all means you need to add other components alongside the webView, then use a view fragment <>

<>
 <Text>Hello Webview</>
 <Webview source={{uri:"www.google.com"}} style={{flex:1}} />
</>
0

for me, after adding flex: 1 for the <WebView> or its parent, it was show in some cases, but in case is not showing, I discovered that the WebView has a chache which causing the latest html not showing. so I ended up to add cacheEnabled={false} property and it work perfectly like example below.

         <WebView
            style={{ flex: 1 }}
            originWhitelist={["*"]}
            cacheEnabled={false}
            source={{
              uri: "https://google.com",
            }}
          />
-1

Below is piece of the code which worked for me.

render: function(){
   return(
     <View style={styles.container}>
      <WebView url={'https://m.facebook.com'} style= {styles.webView}/>
     </View>
   );
 }
1
  • 18
    You forgot to mention which styles have used
    – piotr_cz
    Commented Jun 27, 2017 at 11:42
-1

import { WebView } from 'react-native'; is deprecated

use below line instead

npm install [email protected] --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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.