0
0
React Nativemobile~20 mins

Camera access (expo-camera) in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Camera Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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 }} />;
}
AThe app crashes with a runtime error.
BThe app shows a message: 'No access to camera'.
CThe camera view opens but is black.
DThe app automatically retries permission request forever.
Attempts:
2 left
💡 Hint
Think about how the app handles the permission state when denied.
lifecycle
intermediate
1: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');
  })();
}, []);
AWhen the component unmounts.
BEvery time the component updates.
COnly when the user taps a button.
DWhen the component mounts for the first time.
Attempts:
2 left
💡 Hint
Look at the dependency array of useEffect.
📝 Syntax
advanced
2: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} />
    </>
  );
}
Aconst photo = await Camera.takePictureAsync(); console.log(photo.uri);
Bconst photo = cameraRef.takePicture(); console.log(photo.uri);
Cconst photo = await cameraRef.current.takePictureAsync(); console.log(photo.uri);
Dconst photo = await cameraRef.current.capture(); console.log(photo.uri);
Attempts:
2 left
💡 Hint
Remember to use the ref and the correct method name.
navigation
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();
};
ACall navigation.goBack() after photo is taken.
BCall navigation.navigate('Camera') after photo is taken.
CCall navigation.reset() after photo is taken.
DCall navigation.push('Camera') after photo is taken.
Attempts:
2 left
💡 Hint
Think about returning to the previous screen in the stack.
🔧 Debug
expert
3: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 }} />;
}
AThe Camera component has zero width and height, so preview is invisible.
BThe permission was not actually granted.
CThe Camera component must be wrapped in a ScrollView.
DThe Camera component requires a flashMode prop to show preview.
Attempts:
2 left
💡 Hint
Check the style of the Camera component.