What is react-native-haptic-feedback?
The react-native-haptic-feedback package provides a way to trigger haptic feedback (vibration) on iOS and Android devices. This can be used to enhance user experience by providing tactile feedback for various actions within a React Native application.
What are react-native-haptic-feedback's main functionalities?
Triggering Haptic Feedback
This feature allows you to trigger haptic feedback using the `trigger` method. The example demonstrates how to trigger a light impact haptic feedback when a button is pressed.
import React from 'react';
import { Button, View } from 'react-native';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
const App = () => {
const triggerHapticFeedback = () => {
const options = {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false
};
ReactNativeHapticFeedback.trigger('impactLight', options);
};
return (
<View>
<Button title="Trigger Haptic Feedback" onPress={triggerHapticFeedback} />
</View>
);
};
export default App;
Customizing Haptic Feedback
This feature allows you to customize the type of haptic feedback. The example shows how to trigger a 'notificationSuccess' haptic feedback, which can be used to indicate a successful action.
import React from 'react';
import { Button, View } from 'react-native';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
const App = () => {
const triggerCustomHapticFeedback = () => {
const options = {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false
};
ReactNativeHapticFeedback.trigger('notificationSuccess', options);
};
return (
<View>
<Button title="Trigger Custom Haptic Feedback" onPress={triggerCustomHapticFeedback} />
</View>
);
};
export default App;
Other packages similar to react-native-haptic-feedback
react-native-vibration
The react-native-vibration package provides a simple API to trigger vibration on both iOS and Android devices. Unlike react-native-haptic-feedback, it does not offer different types of haptic feedback but focuses on basic vibration functionality.
expo-haptics
The expo-haptics package is part of the Expo ecosystem and provides a comprehensive set of haptic feedback options. It offers more granular control over haptic feedback types compared to react-native-haptic-feedback and is well-integrated with the Expo framework.
react-native-react-native-haptic-feedback
Getting started
$ npm install react-native-haptic-feedback --save
Mostly automatic installation
$ react-native link react-native-haptic-feedback
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
➜ Add Files to [your project's name]
- Go to
node_modules
➜ react-native-haptic-feedback
and add RNReactNativeHapticFeedback.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNReactNativeHapticFeedback.a
to your project's Build Phases
➜ Link Binary With Libraries
- Run your project (
Cmd+R
)<
Android
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.reactlibrary.RNReactNativeHapticFeedbackPackage;
to the imports at the top of the file - Add
new RNReactNativeHapticFeedbackPackage()
to the list returned by the getPackages()
method
- Append the following lines to
android/settings.gradle
:
include ':react-native-haptic-feedback'
project(':react-native-haptic-feedback').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-haptic-feedback/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:
compile project(':react-native-haptic-feedback')
Usage
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
ReactNativeHapticFeedback.trigger('impactLight');
Available methods
Method | Description |
---|
trigger (method) | Trigger haptic feedback. Possible values are "selection", "impactLight", "impactMedium", "impactHeavy", "notificationSuccess", "notificationWarning", "notificationError" (default: "selection") |