Introduction
Knowing device info helps your app adjust to different phones. Haptics give users a gentle vibration to feel feedback.
Knowing device info helps your app adjust to different phones. Haptics give users a gentle vibration to feel feedback.
import { Platform, Dimensions, Vibration } from 'react-native'; // Get device info const os = Platform.OS; // 'ios' or 'android' const version = Platform.Version; // OS version number const { width, height } = Dimensions.get('window'); // Use haptics Vibration.vibrate(100); // vibrate for 100 milliseconds
Platform tells you the OS type and version.
Dimensions gives screen size to help layout.
Vibration triggers device vibration for feedback.
const os = Platform.OS; console.log(os);
const { width, height } = Dimensions.get('window');
console.log(`Width: ${width}, Height: ${height}`);Vibration.vibrate(200);This app shows the device OS, version, and screen size. Pressing the button makes the device vibrate shortly.
import React from 'react'; import { View, Text, Button, Platform, Dimensions, Vibration, StyleSheet } from 'react-native'; export default function DeviceInfoHaptics() { const os = Platform.OS; const version = Platform.Version; const { width, height } = Dimensions.get('window'); const handleVibrate = () => { Vibration.vibrate(150); }; return ( <View style={styles.container} accessible={true} accessibilityLabel="Device info and haptics screen"> <Text style={styles.text}>OS: {os}</Text> <Text style={styles.text}>Version: {version}</Text> <Text style={styles.text}>Screen: {width} x {height}</Text> <Button title="Vibrate Device" onPress={handleVibrate} accessibilityLabel="Button to vibrate device" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20, backgroundColor: '#f0f0f0' }, text: { fontSize: 18, marginBottom: 10 } });
Not all devices support vibration; test before using.
Use Platform.OS to write OS-specific code.
Screen size helps make your app look good on all devices.
Use Platform to get OS info.
Use Dimensions to get screen size.
Use Vibration to give tactile feedback.
Platform module in React Native gives details about the device OS like 'ios' or 'android'.Dimensions gives screen size, Vibration controls haptic feedback, and StatusBar manages the status bar UI, not OS info.Dimensions.get('screen'), which returns an object with width and height..width property, so Dimensions.get('screen').width is correct syntax.import { Vibration } from 'react-native';
Vibration.vibrate(1000);
Choose the correct behavior.Vibration.vibrate() makes the device vibrate for that many milliseconds (1000 ms = 1 second).import { Vibration } from 'react-native';
Vibration.vibrate([500, 1000]);true is needed. Without it, the pattern runs once, so no error but possibly unintended behavior.[500, 300, 500, 300].Vibration.vibrate([500, 300, 500, 300], false) executes the sequence once without looping further.true causes indefinite looping (cancel timing imprecise); shorter pattern loops incorrectly; separate calls lack pauses.Pass the test and this pattern moves unknown