Challenge - 5 Problems
Location Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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> ); }
Attempts:
2 left
💡 Hint
Think about what
Geolocation.getCurrentPosition does when it succeeds.✗ Incorrect
The
getCurrentPosition function calls the success callback with an object containing coords that has latitude and longitude. This updates the location state with these coordinates, so the UI shows them as a JSON string.🧠 Conceptual
intermediate1: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?Attempts:
2 left
💡 Hint
Location permission is about GPS or precise location.
✗ Incorrect
The
ACCESS_FINE_LOCATION permission allows the app to access precise location data from GPS or network sources. Other permissions like INTERNET or CAMERA are unrelated to location.❓ lifecycle
advanced2: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?Attempts:
2 left
💡 Hint
Think about what happens when background tasks are not stopped.
✗ Incorrect
If you don't clear the watch, the location listener stays active, causing battery drain and unnecessary resource use. It does not stop automatically or crash the app immediately.
📝 Syntax
advanced1: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?
Attempts:
2 left
💡 Hint
Check the exact method and constant names from the library.
✗ Incorrect
The correct usage is to import
request and PERMISSIONS from the library and call request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE). Other options use wrong method names or string formats.🔧 Debug
expert2: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> ); }
Attempts:
2 left
💡 Hint
Remember how React state updates work.
✗ Incorrect
React state variables must be updated using their setter functions. Direct assignment to the state variable does not trigger a re-render or update the UI.