How To Use WebViews in a React Native App


As a React Native developer, you are going to come across some use cases that will require you to embed or redirect a web application or a web page inside a React Native app. WebViews are often used for such use cases.

A community-maintained module, WebViews in React Native are the only way to let the user visit external links within an iOS or Android application. The WebView component in React Native core first became available in React Native version 0.57.x.

In this tutorial, you are going to learn how to utilize a primary WebView component using react-native-webview, which is the official cross-platform module to attain the functionality. After understanding how a simple WebView component works, you will leverage react-navigation to showcase a pragmatic approach that can be useful in real-time React Native applications.


To follow this tutorial, make sure you have the following installed:

Getting Started

To start, you first have to initialize a new React Native project. Run the following command to create a new project and then navigate inside the generated directory. Note that, by default, react-native-cli uses yarn as the package manager.

1# to generate a project directory
2react-native-cli init RNWebviewsDemo
4# navigate inside the project directory
5cd RNWebViewsDemo

To use a WebView component, you have to install the react-native-webview package. Also, make sure you link the native bindings with the newly installed dependency.

1# to install
2yarn add react-native-webview
4# to link
5react-native link react-native-webview

An iOS application does not require any further steps to make this work. For Android platforms version 6.x.x and above, you also have to open android/ and add the following two lines.


Once the dependency installation is done, let us run the application. We are going to use an iOS simulator for this tutorial. If you are on Windows or Linux based operating systems, you can use Android Studio.

Run the command as stated below to open the boilerplate application that comes with react-native-cli.

1# for Mac users
2react-native run-ios
4# for Windows/Linux users
5react-native run-android

The following screen will welcome you:

Welcome Screen

A Simple WebView

Let us start by opening and modifying App.js file. In this file, import the WebView component from react-native-webview.

1import React, { Component } from 'react'
2import WebView from 'react-native-webview'
4export default class App extends Component {
5 render() {
6 return (
7 <WebView
8 source={{ uri: '' }}
9 style={{ marginTop: 20 }}
10 />
11 )
12 }

The WebView component requires a source prop. This prop loads the static HTML or a URI (which is the current case if you look closely at the above snippet). A URI is a remote location for a web page to exist. A static HTML will be some internal HTML file that embeds some content to display. The style prop is basic React Native inline styling that allows you to customize a WebView.

If your simulator is still running, you will get the following result.


In the above demo, you can see that the simplest WebView component you wrote earlier works. It loads the complete website, but there are some flaws. First, it does not have a navigation button to navigate back to the application. Second, it does not have a loading indicator that demonstrates that the web page is still loading. From the next section onwards, you are going to add these two functionalities.

Setting up react-navigation

To add a navigator in the current React Native application, you have to install the following dependencies and link them.

1# to install
2yarn add react-navigation react-native-gesture-handler
4# to link
5react-native link react-native-gesture-handler

Again, no further steps are required to make react-navigation library work with iOS; but with Android, you have to make the following modifications inside the file android/app/src/main/java/com/rnwebviewsdemo/

After other import statements, add the following snippet.

1import com.facebook.react.ReactActivityDelegate;
2import com.facebook.react.ReactRootView;
3import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

Then, in the same file, inside the class MainActivity add the following snippet.

2 protected ReactActivityDelegate createReactActivityDelegate() {
3 return new ReactActivityDelegate(this, getMainComponentName()) {
4 @Override
5 protected ReactRootView createRootView() {
6 return new RNGestureHandlerEnabledRootView(MainActivity.this);
7 }
8 };
9 }

That's it for setting up react-navigation library.

Create a StackNavigator

Now, at the root of the project directory, create a folder called navigation. Inside this folder, create a new file called AppNavigator.js. This file is going to be responsible for initializing screens and create Stack Navigator. Add the import statements that are going to help construct a stack navigator.

1import React from 'react'
2import { createStackNavigator, createAppContainer } from 'react-navigation'
3import Home from '../screens/Home'
5const HomeStack = createStackNavigator({
6 Home: {
7 screen: Home,
8 navigationOptions: ({ navigation }) => ({
9 title: 'Home',
10 headerTintColor: 'blue'
11 })
12 }
15const AppContainer = createAppContainer(HomeStack)
17export default AppContainer

In the above snippet, you are using the createStackNavigator function to create a stack-based navigation flow. This function takes a route configuration object and an options object and returns a React component. Right now, it has only one screen that will be displaying some buttons to navigate to a specific web view. You are going to add the business logic behind HomeScreen in the next section.

Stack Navigation provides a way to transit between screens. This mechanism works quite similar to how a web application works in a web browser. A web app either pushes (next page) or pops (go back) when navigating between different web pages in the browser. Similarly, different screens can be used to either push or pop between in a React Native application.

Each route is expected to have a screen name. For example, in the above snippet, there is a Home route with the same screen name. This screen name is provided using the attribute title that is coming from navigationOptions. To modify the presentation of a screen in the react-navigation library, options like these are used.

To understand this better, note the two attributes you are currently providing in the above snippet: title and headerTintColor for the Home screen. Later, you will have to define the screen to display Web View. You will have similar options for that second screen but with different properties.

Lastly, open the App.js file to add the AppContainer. The App component in any React Native application is always the entry point. To make navigation work, you have to modify the file, as shown below.

1import React from 'react'
2import AppContainer from './navigation/AppNavigator'
4export default function App() {
5 return <AppContainer />

Adding a Home Screen

Create a new directory called screens with a file name Home.js. This file will represent the first or initial screen whenever the application starts.

1import React, { Component } from 'react'
2import { View, Text, StyleSheet } from 'react-native'
4class Home extends Component {
5 render() {
6 return (
7 <View style={styles.container}>
8 <Text style={styles.text}>Home Screen</Text>
9 </View>
10 )
11 }
14const styles = StyleSheet.create({
15 container: {
16 flex: 1,
17 backgroundColor: '#fff',
18 alignItems: 'center',
19 justifyContent: 'center'
20 },
21 text: {
22 color: '#333',
23 textAlign: 'center'
24 }
27export default Home

So far, it is a simple React Native component that displays a text field. Go to the simulator, and you will see the following result.


Now, as the Home screen component is working, you can start working on the main application. Start by defining a state object that contains some URLs in the form of an array inside the Home component.

1state = {
2 links: [
3 { title: 'Jscrambler Blog', link: '' },
4 { title: 'My Portfolio', link: '' }
5 ]

Each URL has a title that will be displayed. The next step is to map this array to display different buttons. These buttons will then point to the correct URL, and that URL will be rendered through a WebView component. Here is the complete code for the Home component.

1class Home extends Component {
2 state = {
3 links: [
4 { title: 'JScrambler Blog', link: '' },
5 { title: 'My Portfolio', link: '' }
6 ]
7 }
9 handleButtonPress(item) {
10 const { title, link } = item
11 this.props.navigation.navigate('Browser', { title, link })
12 }
14 render() {
15 return (
16 <View style={styles.container}>
17 <View style={styles.list}>
18 {, index) => (
19 <TouchableOpacity
20 key={index}
21 onPress={() => this.handleButtonPress(item)}
22 style={styles.button}
23 >
24 <Text style={styles.text}>{item.title}</Text>
25 </TouchableOpacity>
26 ))}
27 </View>
28 </View>
29 )
30 }
33const styles = StyleSheet.create({
34 container: {
35 flex: 1,
36 backgroundColor: '#fff',
37 alignItems: 'center',
38 justifyContent: 'center'
39 },
40 buttonList: {
41 flex: 1,
42 justifyContent: 'center'
43 },
44 button: {
45 margin: 10,
46 backgroundColor: '#356bca',
47 borderRadius: 5,
48 padding: 10
49 },
50 text: {
51 color: '#fff',
52 textAlign: 'center'
53 }
56export default Home

The handleButtonPress event takes each object from the state and using props.navigation.navigate it figures out that when the button is pressed, the app user has to be navigated on the next screen or route called Browser. This is where react-navigation comes to use. When navigating the to the next screen, you are also passing the link and the title of each web view from the state. These values can be easily obtained in the Browser screen using navigation parameters.

You will get the following result in the simulator running the application.


Create the Browser Screen

Inside screens folder, create a new file called Browser.js This will hold the component Browser which is going to be responsible for navigating to the correct WebView, dynamically.

First, you have to hook this new screen in the AppNavigator.js file. To make use of navigation props, you have to specify each screen. Modify the HomeStack navigator as below.

1// after other imports ...
2import Browser from '../screens/Browser'
4// ... modify only the HomeStack
6const HomeStack = createStackNavigator({
7 Home: {
8 screen: Home,
9 navigationOptions: ({ navigation }) => ({
10 title: 'Home',
11 headerTintColor: 'blue'
12 })
13 },
14 Browser: {
15 screen: Browser,
16 navigationOptions: ({ navigation }) => ({
17 title: navigation.state.params.title,
18 headerTintColor: 'green'
19 })
20 }

In the above snippet, notice how each Browser screen will have dynamic title coming and a tint color property that’s different from the Home screen.

Now, open Browser.js and add the following logic to it. Start by importing the WebView component from react-native-webview.

1import React, { Component } from 'react'
2import { WebView } from 'react-native-webview'
4class Browser extends Component {
5 render() {
6 const { params } = this.props.navigation.state
8 return <WebView source={{ uri: }} />
9 }
12export default Browser

In the above snippet, you are fetching parameters. The this.props.navigation.state.params allows reading the parameters you passed through when handling the button press in the Home screen component. This params object can be then used to render the value of each web view link.

Now, if you click on any of the buttons, you will notice that it opens a new screen that displays the web page. Do note the color change in the title. Also, the stack navigator by default gives a way to exit the web view whenever required by providing a back button. See the below demo for detailed information.


The web view integration is working now. However, there is still no way to indicate how much time a webpage will completely take to load. Let us add the functionality in the next section.

Adding ActivityIndicator to WebView

To add an ActivityIndicator to a WebViewcomponent you have to first define an ActivityIndicator. Import this component from react-native core. Then, modify the Browser.js file as following.

1import React, { Component } from 'react'
2import { WebView } from 'react-native-webview'
3import { ActivityIndicator, StyleSheet } from 'react-native'
5class Browser extends Component {
6 LoadingIndicatorView() {
7 return (
8 <ActivityIndicator
9 color="#009b88"
10 size="large"
11 style={styles.ActivityIndicatorStyle}
12 />
13 )
14 }
16 render() {
17 const { params } = this.props.navigation.state
19 return (
20 <WebView
21 source={{ uri: }}
22 renderLoading={this.LoadingIndicatorView}
23 startInLoadingState={true}
24 />
25 )
26 }
29const styles = StyleSheet.create({
30 ActivityIndicatorStyle: {
31 flex: 1,
32 justifyContent: 'center'
33 }
36export default Browser

Once you have added the ActivityIndicator, the WebView component requires two props to make it work. First, the startInLoadingState prop must be set to true, as shown in the above snippet. Then, the renderLoading prop is responsible for triggering the loading indicator, in our case, the function: LoadingIndicatorView(). This prop always accepts a function as its value. Both of these props are not available by default. You have to specify them explicitly.

Take a look at how it works in the below screen.



This concludes the tutorial on getting started, setting up, and using a WebView component in a React Native application. The important part of this tutorial is to make use of react-navigation library to provide a complete navigational flow between different screens and a WebView screen.

Your challenge now is to extend the knowledge you have gained in this tutorial about WebView components and extend it further. An excellent place to start is by referring its official API documentation here.

You will find the complete code for this tutorial at this Github repository.

Originally published at Jscrambler

I'm Aman working as an independent fullstack developer with technologies such as Node.js, ReactJS, and React Native. I try to document and write tutorials to help JavaScript, Web and Mobile developers.