Load Local HTML File or URL in React Native using react-native-webview

Load Local HTML File or URL in React Native using react-native-webview is the updated and combined post of our last 2 posts Open any Website in React Native WebView and React Native Load Local HTML File in WebView.

In this example, we will use the WebView component from react-native-webview. We will make a single page where we will have 3 buttons which will load the data in WebView from different resources.

Different tasks that you can see in this example are:

1. Loading of WebPage from URL

2. Load HTML Page from a variable with HTML String

3. Load a Local HTML File

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use WebView you need to install react-native-webview dependency.
To install this dependency open the terminal and jump into your project

Run the following commands

This command will copy the dependency into your node_module directory. –save is optional, it is just to update dependency in your package.json file.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use

Project File Structure

If you want to load the HTML from the local HTML file into the WebView then you need to put them in the below-described directory.  You have to make 2 copies of the HTML file and have to put them on a different location for Android and IOS. If the directories are not there then please make the directory and then put the HTML file.

1. Android will load the external HTML from project>android>app>src>main>assets>index.html
2. IOS will load the external HTML from project>resources>index.html

Now open App.js in any code editor and replace the code with the following code.

App.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

IOS

      

Android

      

If you need to load the external js file then you can follow the below instruction provided by one of our AboutReact family member Ha Thanh Tam.

At android folder, we can load static js as normal
Android: <script src=”Chart.min.js”></script>

However, for ios, it needs to put the absolute path as from where js class with Webview reference to js library
iOS: <script src=”../../../../resources/Chart.min.js”></script>

This is how you can Load Local HTML File or URL in React Native using react-native-webview. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

8 thoughts on “Load Local HTML File or URL in React Native using react-native-webview”

  1. Hi,
    Your article has been pretty helpful. One thing I wanted to know, how can I add a back button, so that when I press it .This takes me to the previous page within the application
    Thank You
    -Asif Mahmud

  2. Thanks for the example, It is working as expected.

    However, in local html, i cant load the chart js library.
    If i replace it by a cdn link, it working fine.

    So it could be something else to configure with the url for local js file ?

      • If you need to load the external js file then you can follow the below instruction provided by one of our AboutReact family member Ha Thanh Tam.

        At android folder, we can load static js as normal
        Android: <script src=”Chart.min.js”</script>

        However, for ios, it needs to put the absolute path as from where js class with Webview reference to js library
        iOS: <script src=”../../../../resources/Chart.min.js”</script>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.