Contents
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.
Tasks
Different tasks that you can see in this example are:
1. Loading of WebPage from URL
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={{ uri: "https://aboutreact.com" }}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
2. Load HTML Page from a variable with HTML String
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={{ html: '<h1>Hello</h1>' }}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
3. Load a Local HTML File
if(isAndroid){
return (
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={{uri:'file:///android_asset/index.html'}}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
)
}else{
return(
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={'./resources/index.html'}
style={{ marginTop: 20 }}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
}
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
cd ProjectName
Run the following commands
npm install react-native-webview --save
This command will copy the dependency into your node_module directory. –save is optional, it is just to update the 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
cd ios && pod install && cd ..
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.
Code
Now open App.js in any code editor and replace the code with the following code
App.js
// Load Local HTML File or URL in React Native using react-native-webview
// https://aboutreact.com/load-local-html-file-url-using-react-native-webview/
//import React in our code
import React, {useState} from 'react';
//import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
View,
Button,
Platform
} from 'react-native';
//import WebView
import {WebView} from 'react-native-webview';
import HTML_FILE from './resources/index.html';
const isAndroid = Platform.OS === 'android';
const RenderWebViewElement = (props) => {
let {index} = props;
if (index === 1) {
// Loading data from URL into webview
return (
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={{uri: 'https://aboutreact.com'}}
style={{marginTop: 20}}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
} else if (index === 2) {
// Load HTML directly (For simple HTML text)
return (
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={{html: '<h1>Hello</h1>'}}
style={{marginTop: 20}}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
} else {
// Load HTML from a local file
if (isAndroid) {
return (
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={{
uri: 'file:///android_asset/index.html'
}}
style={{marginTop: 20}}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
} else {
return (
<WebView
style={{flex: 1}}
originWhitelist={['*']}
source={HTML_FILE}
style={{marginTop: 20}}
javaScriptEnabled={true}
domStorageEnabled={true}
/>
);
}
}
};
const App = () => {
const [fragmentIndex, setFragmentIndex] = useState(1);
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<Button
title="Load from URL"
onPress={() => setFragmentIndex(1)} />
<Button
title="Load HTML Text"
onPress={() => setFragmentIndex(2)} />
<Button
title="Load from Local HTML FIle"
onPress={() => setFragmentIndex(3)}
/>
<RenderWebViewElement index={fragmentIndex} />
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: '#F5FCFF',
flex: 1,
},
});
export default App;
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 (macOS only)
react-native run-ios
Output Screenshots
IOS
Android
This is how you can Load Local HTML File or URL in React Native using react-native-webview. If you have any doubts 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. 🙂
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
You can add a general button and can navigate to old screen normally.
Here is the example to help you https://aboutreact.com/react-native-navigation-router-version-3/
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 ?
Hello, I have tried many ways to achieve that but everything is waste. I am still in search of that. Sorry, but I can’t help you more than that.
I have the same problem. Did you find solution?
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>
import HTML_FILE from “./resources/index.html”;
Does this even work? as we are not writing export anywhere in index.html file
I’m managing to download a folder, then show the contained html via webview, The html even has js and css files(also contained in the zip file).
Sorry, I am unable to find any solution to visualize the external CSS and JS in WebView.
Hi! I tried your solution and I have a question. I’m trying to load my html file, but I get “Error loading page. Domain: undefined. Error code: -1. Description: net::ERR_ACCESS_DENIED”
Do you know how to fix this? I’m new to this and I have no idea what I’ve done wrong.
Thanks in advance 🙂