Bird
Raised Fist0
React Nativemobile~20 mins

Device info and haptics in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Device Info and Haptics
This screen shows basic device information and has a button that triggers a vibration when pressed.
Target UI
-------------------------
| Device Info and Haptics |
-------------------------

Device Brand: 
Device Model: 
System Version: 

[ Vibrate Button ]
Display device brand, model, and system version using react-native-device-info
Add a button labeled 'Vibrate Button'
When the button is pressed, trigger a short vibration using React Native's Vibration API
Use functional components and hooks
Ensure the UI is simple and readable
Starter Code
React Native
import React, { useState, useEffect } from 'react';
import { View, Text, Button, Vibration, StyleSheet } from 'react-native';
import DeviceInfo from 'react-native-device-info';

export default function DeviceInfoHaptics() {
  const [brand, setBrand] = useState('');
  const [model, setModel] = useState('');
  const [systemVersion, setSystemVersion] = useState('');

  useEffect(() => {
    // TODO: Get device info here
  }, []);

  const handleVibrate = () => {
    // TODO: Trigger vibration here
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Device Info and Haptics</Text>
      <Text>Device Brand: {brand}</Text>
      <Text>Device Model: {model}</Text>
      <Text>System Version: {systemVersion}</Text>
      <View style={styles.buttonContainer}>
        <Button title="Vibrate Button" onPress={handleVibrate} />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 20
  },
  title: {
    fontSize: 20,
    fontWeight: 'bold',
    marginBottom: 20
  },
  buttonContainer: {
    marginTop: 30,
    width: '60%'
  }
});
Task 1
Task 2
Solution
React Native
import React, { useState, useEffect } from 'react';
import { View, Text, Button, Vibration, StyleSheet } from 'react-native';
import DeviceInfo from 'react-native-device-info';

export default function DeviceInfoHaptics() {
  const [brand, setBrand] = useState('');
  const [model, setModel] = useState('');
  const [systemVersion, setSystemVersion] = useState('');

  useEffect(() => {
    setBrand(DeviceInfo.getBrand());
    setModel(DeviceInfo.getModel());
    setSystemVersion(DeviceInfo.getSystemVersion());
  }, []);

  const handleVibrate = () => {
    Vibration.vibrate(100); // vibrate for 100 milliseconds
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Device Info and Haptics</Text>
      <Text>Device Brand: {brand}</Text>
      <Text>Device Model: {model}</Text>
      <Text>System Version: {systemVersion}</Text>
      <View style={styles.buttonContainer}>
        <Button title="Vibrate Button" onPress={handleVibrate} accessibilityLabel="Trigger device vibration" />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 20
  },
  title: {
    fontSize: 20,
    fontWeight: 'bold',
    marginBottom: 20
  },
  buttonContainer: {
    marginTop: 30,
    width: '60%'
  }
});

We use react-native-device-info to get device details like brand, model, and system version. These values are stored in state variables and shown on screen.

The useEffect hook runs once when the component loads to fetch and set this info.

The handleVibrate function uses React Native's Vibration.vibrate() method to make the device vibrate for 100 milliseconds when the button is pressed.

The UI is simple with text labels and a button, styled for clarity and accessibility.

Final Result
Completed Screen
-------------------------
| Device Info and Haptics |
-------------------------

Device Brand: Apple
Device Model: iPhone 13
System Version: 16.4

[ Vibrate Button ]
When the screen loads, device info text updates with actual device data.
Pressing the 'Vibrate Button' causes the device to vibrate shortly.
Stretch Goal
Add a toggle switch to enable or disable vibration feedback.
💡 Hint
Use React Native's Switch component and conditionally call Vibration.vibrate() only when enabled.

Practice

(1/5)
1. Which React Native module provides information about the device's operating system?
easy
A. Platform
B. Dimensions
C. Vibration
D. StatusBar

Solution

  1. Step 1: Identify the module for OS info

    The Platform module in React Native gives details about the device OS like 'ios' or 'android'.
  2. Step 2: Compare with other modules

    Dimensions gives screen size, Vibration controls haptic feedback, and StatusBar manages the status bar UI, not OS info.
  3. Final Answer:

    Platform -> Option A
  4. Quick Check:

    OS info = Platform [OK]
Hint: Platform module always gives OS info in React Native [OK]
Common Mistakes:
  • Confusing Dimensions with OS info
  • Thinking Vibration provides device info
  • Assuming StatusBar relates to OS details
2. Which of the following is the correct syntax to get the screen width using React Native's Dimensions module?
easy
A. Dimensions.screen.width()
B. Dimensions.width()
C. Dimensions.get('screen').width
D. Dimensions.getWidth('screen')

Solution

  1. Step 1: Recall Dimensions API usage

    The correct method to get screen size is Dimensions.get('screen'), which returns an object with width and height.
  2. Step 2: Access width property correctly

    Access the width by .width property, so Dimensions.get('screen').width is correct syntax.
  3. Final Answer:

    Dimensions.get('screen').width -> Option C
  4. Quick Check:

    Screen width = Dimensions.get('screen').width [OK]
Hint: Use Dimensions.get('screen').width to get screen width [OK]
Common Mistakes:
  • Calling width as a function instead of property
  • Using wrong method names like getWidth
  • Accessing screen as a property instead of argument
3. What will be the result of this React Native code snippet?
import { Vibration } from 'react-native';

Vibration.vibrate(1000);
Choose the correct behavior.
medium
A. The device vibrates for 1 second
B. The device vibrates indefinitely until stopped
C. The device vibrates for 100 milliseconds
D. No vibration occurs because vibrate needs a pattern array

Solution

  1. Step 1: Understand Vibration.vibrate parameter

    Passing a number like 1000 to Vibration.vibrate() makes the device vibrate for that many milliseconds (1000 ms = 1 second).
  2. Step 2: Check other options

    Indefinite vibration requires a pattern with repeat, 100 ms is too short, and vibrate accepts a number directly, so no pattern array is needed.
  3. Final Answer:

    The device vibrates for 1 second -> Option A
  4. Quick Check:

    Vibration.vibrate(1000) = 1 second vibration [OK]
Hint: Number argument in vibrate() is vibration duration in ms [OK]
Common Mistakes:
  • Thinking vibrate needs a pattern array always
  • Confusing milliseconds with seconds
  • Assuming vibrate runs indefinitely by default
4. Identify the error in this React Native code snippet that tries to vibrate the device:
import { Vibration } from 'react-native';

Vibration.vibrate([500, 1000]);
medium
A. The vibrate method requires a second argument for repeat
B. The array pattern is correct but missing a repeat flag
C. The array should be passed as a second argument, not the first
D. The vibrate method does not accept an array as argument

Solution

  1. Step 1: Check Vibration.vibrate pattern usage

    Vibration.vibrate accepts an array pattern as first argument to define vibration and pause durations.
  2. Step 2: Understand repeat flag necessity

    To repeat the pattern, a second boolean argument true is needed. Without it, the pattern runs once, so no error but possibly unintended behavior.
  3. Final Answer:

    The array pattern is correct but missing a repeat flag -> Option B
  4. Quick Check:

    Pattern array needs repeat flag to loop [OK]
Hint: Add second argument true to repeat vibration pattern [OK]
Common Mistakes:
  • Thinking array is invalid argument
  • Forgetting second argument for repeat
  • Passing array as second argument instead of first
5. You want to create a React Native function that vibrates the device for 500ms, then pauses for 300ms, and repeats this pattern twice. Which code snippet correctly implements this behavior?
hard
A. Vibration.vibrate(500); Vibration.vibrate(300); Vibration.vibrate(500);
B. Vibration.vibrate([500, 300, 500, 300], true); setTimeout(() => Vibration.cancel(), 2600);
C. Vibration.vibrate([500, 300], true); setTimeout(() => Vibration.cancel(), 2600);
D. Vibration.vibrate([500, 300, 500, 300], false);

Solution

  1. Step 1: Understand the required pattern

    The pattern is vibrate 500ms, pause 300ms, repeated twice: [500, 300, 500, 300].
  2. Step 2: Use repeat false to play exactly twice

    Vibration.vibrate([500, 300, 500, 300], false) executes the sequence once without looping further.
  3. Step 3: Eliminate other options

    Using true causes indefinite looping (cancel timing imprecise); shorter pattern loops incorrectly; separate calls lack pauses.
  4. Final Answer:

    Vibration.vibrate([500, 300, 500, 300], false); -> Option D
  5. Quick Check:

    Two pattern repeats = [500, 300, 500, 300], false [OK]
Hint: Unroll pattern twice with repeat false for exact repeats [OK]
Common Mistakes:
  • Using repeat true without precise cancel
  • Using false for repeat flag
  • Calling vibrate multiple times instead of pattern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard