Challenge - 5 Problems
Camera Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when the camera permission is denied?
Consider a React Native app using expo-camera. If the user denies camera permission, what will the app show?
React Native
import { Camera } from 'expo-camera'; import React, { useState, useEffect } from 'react'; import { View, Text } from 'react-native'; export default function App() { const [hasPermission, setHasPermission] = useState(null); useEffect(() => { (async () => { const { status } = await Camera.requestCameraPermissionsAsync(); setHasPermission(status === 'granted'); })(); }, []); if (hasPermission === null) { return <View><Text>Requesting permission...</Text></View>; } if (hasPermission === false) { return <View><Text>No access to camera</Text></View>; } return <Camera style={{ flex: 1 }} />; }
Attempts:
2 left
💡 Hint
Think about how the app handles the permission state when denied.
✗ Incorrect
When permission is denied, the state hasPermission is false, so the app shows the message 'No access to camera'. It does not crash or retry automatically.
❓ lifecycle
intermediate1:30remaining
When is the camera permission requested in the component lifecycle?
In a React Native component using expo-camera, when does the permission request happen?
React Native
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);Attempts:
2 left
💡 Hint
Look at the dependency array of useEffect.
✗ Incorrect
The empty dependency array [] means the effect runs once after the component mounts.
📝 Syntax
advanced2:30remaining
Which code correctly captures a photo using expo-camera?
Select the code snippet that correctly takes a picture with expo-camera and saves the URI.
React Native
import { Camera } from 'expo-camera'; import React, { useRef } from 'react'; import { Button } from 'react-native'; export default function App() { const cameraRef = useRef(null); const takePicture = async () => { if (cameraRef.current) { const photo = await cameraRef.current.takePictureAsync(); console.log(photo.uri); } }; return ( <> <Camera ref={cameraRef} style={{ flex: 1 }} /> <Button title="Snap" onPress={takePicture} /> </> ); }
Attempts:
2 left
💡 Hint
Remember to use the ref and the correct method name.
✗ Incorrect
The correct method is takePictureAsync() called on the camera ref current object. Other options use wrong method names or call on wrong objects.
advanced
2:00remaining
How to navigate back after taking a photo in a camera screen?
In a React Navigation stack, after taking a photo with expo-camera, how do you navigate back to the previous screen?
React Native
import { useNavigation } from '@react-navigation/native'; const navigation = useNavigation(); const takePicture = async () => { const photo = await cameraRef.current.takePictureAsync(); navigation.goBack(); };
Attempts:
2 left
💡 Hint
Think about returning to the previous screen in the stack.
✗ Incorrect
navigation.goBack() returns to the previous screen. navigate or push would open a new screen. reset clears the navigation state.
🔧 Debug
expert3:00remaining
Why does the camera preview stay black after permission is granted?
You granted camera permission but the preview stays black. What is the most likely cause?
React Native
import { Camera } from 'expo-camera'; import React, { useState, useEffect } from 'react'; import { View } from 'react-native'; export default function App() { const [hasPermission, setHasPermission] = useState(null); useEffect(() => { (async () => { const { status } = await Camera.requestCameraPermissionsAsync(); setHasPermission(status === 'granted'); })(); }, []); if (hasPermission === null) { return <View />; } if (hasPermission === false) { return <View />; } return <Camera style={{ width: 0, height: 0 }} />; }
Attempts:
2 left
💡 Hint
Check the style of the Camera component.
✗ Incorrect
If the Camera has zero width and height, it will not display anything visible, causing a black screen.