Thuta Learning
React Native
AdvancedMobile DevelopmentIntermediate

Working with Device Features (Camera, Location)

React NativeLesson 2225 min

What you'll walk away with

  • Explain the React and native-platform behavior of Working with Device Features (Camera, Location)
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

Explain it simply

Working with Device Features (Camera, Location), in plain language

Before accessing a device feature (camera, location, contacts), you need to request permission from the user (an iOS/Android OS-level requirement) — packages like expo-camera and expo-location provide a requestPermissionsAsync() function, and you have to handle the user's 'Allow'/'Deny' choice. If permission is denied, gracefully disable/explain the feature — the app must not crash.

Problem → Why → Solution

The problem — why this concept matters

Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied

A common mistake

Requesting camera/location permission the moment the app opens (with no clear context) — this makes users suspicious

Core concept — understand how it works

Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

Mental model
  1. request or permission
  2. native module boundary
  3. result, error, or cleanup

Explain the React and native-platform behavior of Working with Device Features (Camera, Location)

Core syntax or pattern

javascript
import * as Location from 'expo-location';
import { useState, useEffect } from 'react';
import { Text } from 'react-native';

function LocationScreen() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      const { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Location permission was denied');
        return;
      }
      const loc = await Location.getCurrentPositionAsync({});
      setLocation(loc);
    })();
  }, []);

  if (errorMsg) return <Text>{errorMsg}</Text>;
  return <Text>{location ? JSON.stringify(location.coords) : 'Loading...'}</Text>;
}

Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

Read the code step by step

  1. request or permissionimport * as Location from 'expo-location';

    Before accessing a device feature (camera, location, contacts), you need to request permission from the user (an iOS/Android OS-level requirement) — packages like expo-camera and expo-location provide a requestPermissionsAsync() function, and you have to handle the user's 'Allow'/'Deny' choice. If permission is denied, gracefully disable/explain the feature — the app must not crash.

  2. native module boundarysetErrorMsg('Location permission was denied');

    Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

  3. result, error, or cleanup}

    Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Execution flow
  1. request or permission
  2. native module boundary
  3. result, error, or cleanup

Success

Explain the React and native-platform behavior of Working with Device Features (Camera, Location)

Failure

Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied

Let's think about it this way for a second

Before accessing a device feature (camera, location, contacts), you need to request permission from the user (an iOS/Android OS-level requirement) — packages like expo-camera and expo-location provide a requestPermissionsAsync() function, and you have to handle the user's 'Allow'/'Deny' choice. If permission is denied, gracefully disable/explain the feature — the app must not crash.

Let's connect this to a real-world scenario

Install expo-location and call Location.requestForegroundPermissionsAsync(), and the OS's native permission dialog will appear — once you get 'Allow', you can fetch GPS coordinates with Location.getCurrentPositionAsync(). Always check the permission status (granted/denied), and if status !== 'granted', show a UI message explaining that the feature is limited.

Code Example

javascript
import * as Location from 'expo-location';
import { useState, useEffect } from 'react';
import { Text } from 'react-native';

function LocationScreen() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      const { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Location permission was denied');
        return;
      }
      const loc = await Location.getCurrentPositionAsync({});
      setLocation(loc);
    })();
  }, []);

  if (errorMsg) return <Text>{errorMsg}</Text>;
  return <Text>{location ? JSON.stringify(location.coords) : 'Loading...'}</Text>;
}
You should see
A permission dialog appears, and tapping 'Allow' displays the device's GPS coordinates on screen.

Try it in 5 minutes

Install expo-location and implement the permission request + current location fetch flow yourself (on the Expo Go app).

A quick word of caution

Location/camera data is sensitive personal information — clearly explain to the user why it's needed before requesting permission, and if you send it to a backend, follow the encryption/HTTPS concepts from the Cybersecurity tutorial.

Real-world usage

How to apply it in a real project

Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Mini challenge

Build it yourself

Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
import * as Location from 'expo-location';
import { useState, useEffect } from 'react';
import { Text } from 'react-native';

function LocationScreen() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      const { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Location permission was denied');
        return;
      }
      const loc = await Location.getCurrentPositionAsync({});
      setLocation(loc);
    })();
  }, []);

  if (errorMsg) return <Text>{errorMsg}</Text>;
  return <Text>{location ? JSON.stringify(location.coords) : 'Loading...'}</Text>;
}

Expected: Explain the React and native-platform behavior of Working with Device Features (Camera, Location)

Show hint

Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Quick quiz 1 / 3

What best describes the core idea of Working with Device Features (Camera, Location)?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • request or permission → native module boundary → result, error, or cleanup
  • Build Working with Device Features (Camera, Location) as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
  • Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied
  • Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

React Native Documentation — React Native

Easy traps

  • Calling getCurrentPositionAsync() right away without checking permission status first, and crashing when it's denied
  • Requesting camera/location permission the moment the app opens (with no clear context) — this makes users suspicious
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

Test an original Working with Device Features (Camera, Location) example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

You'll know it worked when: A permission dialog appears, and tapping 'Allow' displays the device's GPS coordinates on screen.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Working with Device Features (Camera, Location) | Thuta Learning