What if your app could 'feel' the device and respond with a gentle buzz that users love?
Why Device info and haptics in React Native? - Purpose & Use Cases
Imagine you want your app to feel personal and responsive by knowing what device it runs on and giving gentle vibrations when users tap buttons.
Without special tools, you'd have to guess device details and manually code vibration patterns for each phone model.
Manually detecting device info is slow and unreliable because devices vary a lot.
Creating vibration feedback by hand means writing complex code for each device, which is error-prone and takes too much time.
Using device info and haptics libraries in React Native lets you easily get accurate device details and trigger smooth vibrations with simple commands.
This saves time and makes your app feel modern and user-friendly.
if (Platform.OS === 'ios') { // guess device } vibratePattern = [100, 200, 100]; Vibration.vibrate(vibratePattern);
const device = await DeviceInfo.getDeviceName(); Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
You can create apps that adapt to each device and give users satisfying touch feedback effortlessly.
A fitness app that knows if you have an iPhone or Android and vibrates gently when you complete a workout set, making the experience more rewarding.
Manual device detection and vibration coding is slow and unreliable.
Device info and haptics libraries simplify access to device details and vibration features.
This leads to better user experience with less effort.
Practice
Solution
Step 1: Identify the module for OS info
ThePlatformmodule in React Native gives details about the device OS like 'ios' or 'android'.Step 2: Compare with other modules
Dimensionsgives screen size,Vibrationcontrols haptic feedback, andStatusBarmanages the status bar UI, not OS info.Final Answer:
Platform -> Option AQuick Check:
OS info = Platform [OK]
- Confusing Dimensions with OS info
- Thinking Vibration provides device info
- Assuming StatusBar relates to OS details
Solution
Step 1: Recall Dimensions API usage
The correct method to get screen size isDimensions.get('screen'), which returns an object with width and height.Step 2: Access width property correctly
Access the width by.widthproperty, soDimensions.get('screen').widthis correct syntax.Final Answer:
Dimensions.get('screen').width -> Option CQuick Check:
Screen width = Dimensions.get('screen').width [OK]
- Calling width as a function instead of property
- Using wrong method names like getWidth
- Accessing screen as a property instead of argument
import { Vibration } from 'react-native';
Vibration.vibrate(1000);
Choose the correct behavior.Solution
Step 1: Understand Vibration.vibrate parameter
Passing a number like 1000 toVibration.vibrate()makes the device vibrate for that many milliseconds (1000 ms = 1 second).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.Final Answer:
The device vibrates for 1 second -> Option AQuick Check:
Vibration.vibrate(1000) = 1 second vibration [OK]
- Thinking vibrate needs a pattern array always
- Confusing milliseconds with seconds
- Assuming vibrate runs indefinitely by default
import { Vibration } from 'react-native';
Vibration.vibrate([500, 1000]);Solution
Step 1: Check Vibration.vibrate pattern usage
Vibration.vibrate accepts an array pattern as first argument to define vibration and pause durations.Step 2: Understand repeat flag necessity
To repeat the pattern, a second boolean argumenttrueis needed. Without it, the pattern runs once, so no error but possibly unintended behavior.Final Answer:
The array pattern is correct but missing a repeat flag -> Option BQuick Check:
Pattern array needs repeat flag to loop [OK]
- Thinking array is invalid argument
- Forgetting second argument for repeat
- Passing array as second argument instead of first
Solution
Step 1: Understand the required pattern
The pattern is vibrate 500ms, pause 300ms, repeated twice:[500, 300, 500, 300].Step 2: Use repeat false to play exactly twice
Vibration.vibrate([500, 300, 500, 300], false)executes the sequence once without looping further.Step 3: Eliminate other options
Usingtruecauses indefinite looping (cancel timing imprecise); shorter pattern loops incorrectly; separate calls lack pauses.Final Answer:
Vibration.vibrate([500, 300, 500, 300], false); -> Option DQuick Check:
Two pattern repeats = [500, 300, 500, 300], false [OK]
- Using repeat true without precise cancel
- Using false for repeat flag
- Calling vibrate multiple times instead of pattern
Ready to prove it?
Pass the test and this pattern moves unknown
