Expo modules let you add extra features to your React Native app easily. They help you use device tools like camera or sensors without complex setup.
Expo modules in React Native
import { ModuleName } from 'expo-module-name'; // Use the module's functions or components ModuleName.someFunction();
Replace ModuleName and expo-module-name with the actual module you want to use.
Most Expo modules work out of the box with managed Expo projects.
import { Camera } from 'expo-camera'; async function takePicture(cameraRef) { const photo = await cameraRef.current.takePictureAsync(); console.log(photo.uri); }
import * as Location from 'expo-location'; async function getLocation() { const location = await Location.getCurrentPositionAsync({}); console.log(location.coords.latitude, location.coords.longitude); }
This app uses the Expo Location module to get the device's current location when you press the button. It shows latitude and longitude on the screen.
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; import * as Location from 'expo-location'; export default function App() { const [location, setLocation] = useState(null); const [errorMsg, setErrorMsg] = useState(null); async function requestLocation() { let { status } = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { setErrorMsg('Permission to access location was denied'); return; } let loc = await Location.getCurrentPositionAsync({}); setLocation(loc); } return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Button title="Get Location" onPress={requestLocation} /> {location && <Text>Latitude: {location.coords.latitude} Longitude: {location.coords.longitude}</Text>} {errorMsg && <Text>{errorMsg}</Text>} </View> ); }
Always ask for permission before accessing sensitive device features like location or camera.
Expo modules simplify native code setup, so you can focus on JavaScript.
Check Expo documentation for each module to learn its specific functions and usage.
Expo modules add device features to your React Native app easily.
Use them to access camera, location, notifications, and more.
Import the module, ask for permissions if needed, then use its functions.
Practice
Solution
Step 1: Understand Expo modules role
Expo modules provide access to device features such as camera, location, and notifications.Step 2: Compare options with this role
Only To easily add device features like camera and location matches this purpose; others relate to UI, navigation, or backend, which Expo modules do not handle.Final Answer:
To easily add device features like camera and location -> Option DQuick Check:
Expo modules = device features access [OK]
- Confusing Expo modules with navigation libraries
- Thinking Expo modules create UI components
- Assuming Expo modules handle backend logic
Solution
Step 1: Identify the official Expo Camera import
The correct import is from 'expo-camera' with named import { Camera }.Step 2: Check other options for correctness
import Camera from 'react-native-camera'; uses a different library name, C imports from wrong module, D uses a non-existent module name.Final Answer:
import { Camera } from 'expo-camera'; -> Option AQuick Check:
Expo Camera import = { Camera } from 'expo-camera' [OK]
- Using default import instead of named import
- Importing from wrong package name
- Confusing Expo modules with React Native community packages
import { Camera } from 'expo-camera';
import React, { useState, useEffect } from 'react';
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return Requesting permission...;
}
if (hasPermission === false) {
return No access to camera;
}
return Camera access granted;
}Solution
Step 1: Understand permission request flow
The code requests camera permission asynchronously and sets hasPermission to true if granted, false otherwise.Step 2: Analyze user denies permission case
If user denies, status !== 'granted', so hasPermission is false, triggering the second if block to show 'No access to camera'.Final Answer:
Displays 'No access to camera' -> Option AQuick Check:
User denies permission = show 'No access to camera' [OK]
- Assuming permission request never finishes
- Confusing null and false states
- Expecting granted message even if denied
import * as Location from 'expo-location';
import React, { useState, useEffect } from 'react';
export default function App() {
const [location, setLocation] = useState(null);
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
return;
}
let loc = await Location.getCurrentPositionAsync({});
setLocation(loc);
})();
}, []);
return location ? {JSON.stringify(location)} : Loading...;
}Solution
Step 1: Check async function calls
The code uses await Location.requestForegroundPermissionsAsync without parentheses, so it references the function instead of calling it.Step 2: Verify other parts
useState with null is valid, React import is correct, getCurrentPositionAsync can be called with empty options object.Final Answer:
Missing parentheses when calling requestForegroundPermissionsAsync -> Option CQuick Check:
Async function call needs () to execute [OK]
- Forgetting () on async function calls
- Assuming useState(null) is wrong
- Thinking getCurrentPositionAsync requires options
Solution
Step 1: Understand correct usage order
You must import the Location module first to use its functions.Step 2: Request permission before accessing location
Request permission and check status before calling getCurrentPositionAsync to avoid errors.Step 3: Only get and display location if permission is granted
This ensures app behaves correctly and respects user privacy.Final Answer:
Import Location module, request permission, check status, get location if granted, display location -> Option BQuick Check:
Import -> request permission -> check -> get location -> display [OK]
- Trying to get location before permission
- Requesting permission after accessing location
- Importing modules after usage
Ready to prove it?
Pass the test and this pattern moves unknown
