0
0
React Nativemobile~20 mins

Device info and haptics in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Device Info and Haptics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Detecting Device Orientation Change
What will be the output behavior when the device orientation changes in this React Native component?
React Native
import React, { useState, useEffect } from 'react';
import { View, Text, Dimensions } from 'react-native';

export default function Orientation() {
  const [orientation, setOrientation] = useState('portrait');

  useEffect(() => {
    const updateOrientation = () => {
      const { width, height } = Dimensions.get('window');
      setOrientation(width > height ? 'landscape' : 'portrait');
    };
    Dimensions.addEventListener('change', updateOrientation);
    updateOrientation();
    return () => Dimensions.removeEventListener('change', updateOrientation);
  }, []);

  return <View><Text>{`Orientation: ${orientation}`}</Text></View>;
}
AThe text updates to 'Orientation: landscape' or 'Orientation: portrait' when device rotates.
BThe app crashes with a runtime error on orientation change.
CThe text never updates and always shows 'Orientation: portrait'.
DThe text updates but shows incorrect orientation values like 'landscape' when in portrait.
Attempts:
2 left
💡 Hint
Check how Dimensions event listeners update state on orientation change.
ui_behavior
intermediate
1:30remaining
Triggering Haptic Feedback on Button Press
Which option correctly triggers a light haptic feedback when the button is pressed in React Native?
React Native
import React from 'react';
import { Button } from 'react-native';
import * as Haptics from 'expo-haptics';

export default function HapticButton() {
  return <Button title="Press me" onPress={() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)} />;
}
AonPress={() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)}
BonPress={() => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success)}
ConPress={() => Haptics.selectionAsync()}
DonPress={() => Haptics.impactAsync('heavy')}
Attempts:
2 left
💡 Hint
Look for the method that triggers impact feedback with a light style.
lifecycle
advanced
2:30remaining
Cleaning Up Device Orientation Listener
What is the problem with this React Native code regarding device orientation listener cleanup?
React Native
import React, { useEffect, useState } from 'react';
import { Dimensions, Text, View } from 'react-native';

export default function Orientation() {
  const [orientation, setOrientation] = useState('portrait');

  useEffect(() => {
    const handler = () => {
      const { width, height } = Dimensions.get('window');
      setOrientation(width > height ? 'landscape' : 'portrait');
    };
    Dimensions.addEventListener('change', handler);
    return () => {
      Dimensions.removeEventListener('change', handler);
    };
  }, []);

  return <View><Text>{orientation}</Text></View>;
}
AThe code causes a memory leak because the listener is removed twice.
BThe cleanup function removes a different function than the one added, so listener is not removed.
CThe event listener is removed correctly, no problem exists.
DThe handler function is not defined inside useEffect, causing a reference error.
Attempts:
2 left
💡 Hint
Check how the removeEventListener function is called with the handler argument.
📝 Syntax
advanced
1:30remaining
Correct Syntax for Using Haptics in React Native
Which code snippet correctly imports and uses Expo Haptics to trigger a medium impact feedback?
Aimport * as Haptics from 'expo-haptics';\nHaptics.impactAsync(Haptics.ImpactFeedbackStyle.medium);
Bimport Haptics from 'expo-haptics';\nHaptics.impactAsync('medium');
Cimport { impactAsync } from 'expo-haptics';\nimpactAsync('Medium');
Dimport * as Haptics from 'expo-haptics';\nHaptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
Attempts:
2 left
💡 Hint
Check the exact casing of ImpactFeedbackStyle constants and import style.
🧠 Conceptual
expert
1:00remaining
Understanding Device Info API Behavior
What will be the value of the variable 'isTablet' after running this React Native code on a phone device?
React Native
import * as Device from 'expo-device';

const isTablet = Device.isTablet;
Aundefined
Btrue
Cfalse
Dnull
Attempts:
2 left
💡 Hint
Check the meaning of Device.isTablet property in Expo Device API.