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

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. This is because WebView extracted from the react-native core will be removed in the near future so to overcome this problem I have made this new and updated Post.

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

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 dependencies in your package.json file.

Linking of Dependency

After installing the dependency you need to link it with your project using

After linking the dependency if you face Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

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 blow 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. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

6 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 ?

Leave a Comment

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

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

About React will use the information you provide on this form to be in touch with you and to provide updates and marketing.