Home > Article > Web Front-end > Creating a WebView app in React Native using Expo
Creating a WebView app in React Native using Expo is a straightforward process. Below is a step-by-step guide on how to achieve this, including the installation, setting up a WebView, and building the app.
Know More :- https://codexdindia.blogspot.com/2024/07/creating-webview-app-in-react-native.html
First, create a new Expo project using the Expo CLI.
npx create-expo-app WebViewApp --template blank cd WebViewApp
Install the react-native-webview package, which provides the WebView component.
expo install react-native-webview
Create a new component to handle the WebView. Open App.js and replace its content with the following code:
import React from 'react'; import { SafeAreaView, StyleSheet, Platform, ActivityIndicator } from 'react-native'; import { WebView } from 'react-native-webview'; const App = () => { return ( <SafeAreaView style={styles.container}> <WebView source={{ uri: 'https://www.example.com' }} startInLoadingState={true} renderLoading={() => <ActivityIndicator color='blue' size='large' />} style={{ marginTop: Platform.OS === 'ios' ? 20 : 0 }} /> </SafeAreaView> ); }; const styles = StyleSheet.create({ container: { flex: 1, }, }); export default App;
You can customize the WebView further to add more functionalities, such as handling navigation gestures, showing a splash screen, or handling file downloads.
To show a splash screen while the WebView loads, install the Expo Splash Screen package.
expo install expo-splash-screen
Then, modify App.js to use the splash screen:
import React, { useState, useEffect } from 'react'; import { SafeAreaView, StyleSheet, Platform, ActivityIndicator } from 'react-native'; import { WebView } from 'react-native-webview'; import * as SplashScreen from 'expo-splash-screen'; SplashScreen.preventAutoHideAsync(); const App = () => { const [loading, setLoading] = useState(true); useEffect(() => { if (!loading) { SplashScreen.hideAsync(); } }, [loading]); return ( <SafeAreaView style={styles.container}> <WebView source={{ uri: 'https://www.example.com' }} onLoadEnd={() => setLoading(false)} startInLoadingState={true} renderLoading={() => <ActivityIndicator color='blue' size='large' />} style={{ marginTop: Platform.OS === 'ios' ? 20 : 0 }} /> </SafeAreaView> ); }; const styles = StyleSheet.create({ container: { flex: 1, }, }); export default App;
To build and run your app on a physical device, use the following command:
expo start
Scan the QR code with the Expo Go app on your device to see your WebView in action.
By following these steps, you can create a fully functional WebView app in React Native using Expo, with capabilities to handle various web-related functionalities and customizations.
The above is the detailed content of Creating a WebView app in React Native using Expo. For more information, please follow other related articles on the PHP Chinese website!