import React, { useState } from 'react';
import { View, Text, Button, Image, StyleSheet, Vibration, Platform } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
export default function NativeFeaturesDemo() {
const [photoUri, setPhotoUri] = useState(null);
async function takePhoto() {
const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
if (permissionResult.granted === false) {
alert('Camera permission is required to take photos');
return;
}
const result = await ImagePicker.launchCameraAsync({ quality: 0.5 });
if (!result.canceled) {
setPhotoUri(result.assets[0].uri);
}
}
function vibrateDevice() {
if (Platform.OS === 'android' || Platform.OS === 'ios') {
Vibration.vibrate(500);
} else {
alert('Vibration not supported on this platform');
}
}
return (
<View style={styles.container}>
<Text style={styles.title}>Native Features Demo</Text>
<Button title="Take Photo" onPress={takePhoto} />
<View style={{ height: 20 }} />
<Button title="Vibrate Device" onPress={vibrateDevice} />
<View style={{ height: 20 }} />
<Text style={styles.previewLabel}>Photo Preview:</Text>
{photoUri ? (
<Image source={{ uri: photoUri }} style={styles.photo} />
) : (
<Text>No photo taken yet</Text>
)}
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 20, justifyContent: 'flex-start' },
title: { fontSize: 24, fontWeight: 'bold', marginBottom: 20 },
previewLabel: { fontSize: 18, marginBottom: 10 },
photo: { width: 300, height: 300, resizeMode: 'contain' }
});This app uses two native features: the camera and device vibration.
We use expo-image-picker to request camera permission and open the camera. When the user takes a photo, we save its URI and show it on screen.
For vibration, we use React Native's Vibration API to vibrate the device for 500 milliseconds. We check the platform to avoid errors on unsupported devices.
This shows how native features make mobile apps feel special and interactive.