0
0
React Nativemobile~20 mins

Location services in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Location Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this React Native location request code?
Consider this React Native code snippet that requests location permission and fetches the current position. What will be the value of location after the code runs successfully?
React Native
import React, { useState, useEffect } from 'react';
import { View, Text, Button } from 'react-native';
import Geolocation from '@react-native-community/geolocation';

export default function LocationExample() {
  const [location, setLocation] = useState(null);

  const getLocation = () => {
    Geolocation.getCurrentPosition(
      pos => setLocation(pos.coords),
      err => setLocation({ error: err.message })
    );
  };

  useEffect(() => {
    getLocation();
  }, []);

  return (
    <View>
      <Text>{location ? JSON.stringify(location) : 'Loading...'}</Text>
    </View>
  );
}
AAn error object with a message property always, because permission is denied by default
BA string 'Loading...' because location is never set
CNull, because useState was initialized with null and never updated
DAn object with latitude and longitude properties representing the device's current position
Attempts:
2 left
💡 Hint
Think about what Geolocation.getCurrentPosition does when it succeeds.
🧠 Conceptual
intermediate
1:30remaining
Which permission is required to access location on Android in React Native?
To use location services in a React Native app on Android, which permission must be declared in the AndroidManifest.xml file?
A<uses-permission android:name="android.permission.INTERNET" />
B<uses-permission android:name="android.permission.CAMERA" />
C<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
D<uses-permission android:name="android.permission.READ_CONTACTS" />
Attempts:
2 left
💡 Hint
Location permission is about GPS or precise location.
lifecycle
advanced
2:00remaining
What happens if you don't clear location watch in React Native?
In React Native, if you use Geolocation.watchPosition to track location changes but forget to clear the watch with Geolocation.clearWatch, what is the likely effect?
AThe app will continue to consume battery and resources even when the component unmounts
BThe location updates will stop automatically after 5 minutes
CThe app will crash immediately due to memory leak
DNothing happens; watchPosition cleans itself up automatically
Attempts:
2 left
💡 Hint
Think about what happens when background tasks are not stopped.
📝 Syntax
advanced
1:30remaining
Which option correctly requests location permission using React Native Permissions library?
Given the React Native Permissions library, which code snippet correctly requests location permission on iOS?
Aconst result = await request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
Bconst result = await request('ios.permission.LOCATION_WHEN_IN_USE');
Cconst result = await Permissions.request('LOCATION_WHEN_IN_USE');
Dconst result = await requestPermission(PERMISSIONS.IOS.LOCATION_ALWAYS);
Attempts:
2 left
💡 Hint
Check the exact method and constant names from the library.
🔧 Debug
expert
2:30remaining
Why does this React Native location code fail to update state?
This React Native code tries to get location but the UI never updates from 'Loading...'. What is the cause?
React Native
import React, { useState, useEffect } from 'react';
import { View, Text } from 'react-native';
import Geolocation from '@react-native-community/geolocation';

export default function App() {
  const [location, setLocation] = useState(null);

  useEffect(() => {
    Geolocation.getCurrentPosition(position => {
      setLocation(position.coords);
    });
  }, []);

  return (
    <View>
      <Text>{location ? JSON.stringify(location) : 'Loading...'}</Text>
    </View>
  );
}
AThe Geolocation.getCurrentPosition callback is never called due to missing permissions
BThe code assigns to the variable 'location' directly instead of using setLocation, so state does not update
CThe useEffect hook is missing a dependency array, causing infinite re-renders
DThe initial state should be an empty object, not null, to trigger rendering
Attempts:
2 left
💡 Hint
Remember how React state updates work.