Expo supports the Web platform, enabling developers to build web applications using the same codebase. This greatly expands Expo's application scenarios, achieving true cross-platform development.
Expo for Web Features:
- Single Codebase: Use the same JavaScript/TypeScript code
- Responsive Design: Automatically adapts to different screen sizes
- Web API Support: Access browser native APIs
- PWA Support: Can be configured as a Progressive Web App
- Fast Development: Supports hot reloading and fast refresh
Configuring Web Support:
- Install Dependencies:
bashnpx expo install react-dom react-native-web @expo/webpack-config
- Configure app.json:
json{ "expo": { "web": { "bundler": "webpack", "output": "single", "favicon": "./assets/favicon.png" }, "experiments": { "typedRoutes": true } } }
- Start Web Development Server:
bashnpx expo start --web
Platform-Specific Code:
Use the Platform module to handle platform differences:
typescriptimport { Platform } from 'react-native'; function MyComponent() { if (Platform.OS === 'web') { return <div>Web specific content</div>; } return <View>Mobile specific content</View>; }
Web-Specific APIs:
- Window API:
typescript// Get window dimensions const width = window.innerWidth; const height = window.innerHeight; // Listen to window resize window.addEventListener('resize', handleResize);
- Local Storage:
typescript// Use localStorage localStorage.setItem('key', 'value'); const value = localStorage.getItem('key');
- Navigation API:
typescript// Use browser history window.history.pushState({}, '', '/new-route'); window.history.back();
Style Adaptation:
- Responsive Styles:
typescriptimport { StyleSheet, Dimensions } from 'react-native'; const styles = StyleSheet.create({ container: { width: Dimensions.get('window').width > 768 ? '80%' : '100%', padding: 16, }, });
- CSS Media Queries:
typescript// Use expo-linear-gradient and other libraries import { LinearGradient } from 'expo-linear-gradient'; <LinearGradient colors={['#4c669f', '#3b5998']} style={{ flex: 1 }} />
Web-Specific Components:
- HTML Elements:
typescript// Use HTML elements on Web import { View, Text } from 'react-native'; // Renders as div and span on Web <View style={{ padding: 16 }}> <Text>Hello Web</Text> </View>
- Web-Specific Libraries:
typescript// Use react-web-specific libraries import { useMediaQuery } from 'react-responsive'; const isDesktop = useMediaQuery({ minWidth: 992 });
Performance Optimization:
- Code Splitting:
typescript// Use React.lazy for code splitting const LazyComponent = React.lazy(() => import('./LazyComponent'));
- Lazy Loading:
typescript// Lazy load images import { Image } from 'react-native'; <Image source={{ uri: 'https://example.com/image.jpg' }} loading="lazy" />
- Caching Strategy:
typescript// Configure Service Worker for caching // Configure in public/sw.js
PWA Configuration:
- Create manifest.json:
json{ "name": "My Expo App", "short_name": "MyApp", "start_url": "/", "display": "standalone", "background_color": "#ffffff", "theme_color": "#000000", "icons": [ { "src": "/assets/icon-192.png", "sizes": "192x192", "type": "image/png" } ] }
- Configure Service Worker:
javascript// public/sw.js self.addEventListener('install', event => { event.waitUntil( caches.open('v1').then(cache => { return cache.addAll([ '/', '/index.html', '/static/js/main.js' ]); }) ); });
Deploying Web App:
- Build Production Version:
bashnpx expo export:web
- Deploy to Vercel:
bash# Install Vercel CLI npm i -g vercel # Deploy vercel
- Deploy to Netlify:
bash# Install Netlify CLI npm i -g netlify-cli # Deploy netlify deploy --prod
Common Issues:
-
Style Differences: Web and mobile styles may differ, need testing and adjustment
-
API Compatibility: Some mobile APIs are not available on Web, need alternatives
-
Performance Issues: Web version may be slower than mobile, need to optimize loading and rendering
-
Touch Events: Web needs to support both mouse and touch events
-
Keyboard Navigation: Web needs to support keyboard navigation and accessibility
Best Practices:
-
Progressive Enhancement: Implement core features first, then add Web-specific optimizations
-
Responsive Design: Ensure app displays well on different screen sizes
-
Performance Monitoring: Use Web performance tools to monitor and optimize loading speed
-
SEO Optimization: Add meta tags and structured data
-
Test Coverage: Test Web version on multiple browsers and devices
Expo for Web enables developers to build truly cross-platform applications with a single codebase, greatly improving development efficiency and code reuse.