What common native components and APIs does Expo provide? How to use them?
Expo provides rich native components and APIs, enabling developers to easily access various mobile device features. These components and APIs are carefully designed to provide a unified cross-platform interface.
Core Native Components:
- Camera
javascriptimport { Camera } from 'expo-camera'; // Request camera permission const { status } = await Camera.requestCameraPermissionsAsync(); // Use camera component <Camera style={{ flex: 1 }} type={type} />
- Location
javascriptimport * as Location from 'expo-location'; // Request location permission const { status } = await Location.requestForegroundPermissionsAsync(); // Get current location const location = await Location.getCurrentPositionAsync({});
- Notifications
javascriptimport * as Notifications from 'expo-notifications'; // Request notification permission const { status } = await Notifications.requestPermissionsAsync(); // Send local notification await Notifications.scheduleNotificationAsync({ content: { title: 'Hello!', body: 'This is a notification', }, trigger: { seconds: 2 }, });
- Audio
javascriptimport { Audio } from 'expo-av'; // Play audio const { sound } = await Audio.Sound.createAsync( { uri: 'https://example.com/audio.mp3' } ); await sound.playAsync();
- FileSystem
javascriptimport * as FileSystem from 'expo-file-system'; // Read file const content = await FileSystem.readAsStringAsync(fileUri); // Write file await FileSystem.writeAsStringAsync(fileUri, 'Hello World');
- ImagePicker
javascriptimport * as ImagePicker from 'expo-image-picker'; // Select image const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ['images'], allowsEditing: true, });
- SecureStore
javascriptimport * as SecureStore from 'expo-secure-store'; // Save sensitive data await SecureStore.setItemAsync('token', 'user-token'); // Read sensitive data const token = await SecureStore.getItemAsync('token');
- Sensors
javascriptimport { Accelerometer } from 'expo-sensors'; // Listen to accelerometer Accelerometer.addListener(accelerometerData => { console.log(accelerometerData); });
Common APIs:
- Constants
javascriptimport Constants from 'expo-constants'; // Get device information const deviceName = Constants.deviceName; const platform = Constants.platform;
- Haptics
javascriptimport * as Haptics from 'expo-haptics'; // Trigger haptic feedback Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
- Linking
javascriptimport * as Linking from 'expo-linking'; // Open URL await Linking.openURL('https://expo.dev'); // Handle deep linking const url = await Linking.getInitialURL();
- ScreenOrientation
javascriptimport * as ScreenOrientation from 'expo-screen-orientation'; // Lock screen orientation await ScreenOrientation.lockAsync( ScreenOrientation.OrientationLock.PORTRAIT );
Permission Management:
Expo provides a unified permission request API:
javascriptimport * as Permissions from 'expo-permissions'; // Request permissions const { status, granted } = await Permissions.askAsync( Permissions.CAMERA, Permissions.LOCATION );
Best Practices:
-
Permission Request Timing: Request permissions when users need to use the feature, provide clear explanations
-
Error Handling: Properly handle permission denial, provide user-friendly prompts
-
Performance Optimization: Release resources timely, such as stopping sensor listeners, canceling audio playback, etc.
-
Platform Differences: Be aware of API differences across platforms, use conditional rendering for platform-specific features
-
Async Operations: All native API calls are asynchronous, use async/await to handle them
These native components and APIs greatly simplify cross-platform development, allowing developers to focus on business logic rather than native implementation details.