react-native-lantern
Flashlight support on React Native
Warning!!! Support only Android (>= API 23 (>= Android 6.0))
Getting started
npm i react-native-lantern
Automatic installation
npx react-native link react-native-lantern
(use npx react-native unlink react-native-lantern
to uninstall)
Manual installation
Android
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.reactnative.lantern.ReactNativeLanternPackage;
to the imports at the top of the file
- Append the following lines to
android/settings.gradle
:include ':react-native-lantern' project(':react-native-lantern').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-lantern/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-lantern')
Usage
;;; const Main = { const isDisabled setDisabled = ; const isTurnOn setTurnState = ; ; ; const onPress = ; return <View> <Button title=isTurnOn ? 'Off' : 'On' onPress=onPress disabled=isDisabled /> </View> ;}
API
ready() -> Promise
Initialize flashlight. This method should be called at the very beginning, before calling other methods.
turn(turnState) -> Promise
Change turn (on/off).
turnOn() -> Promise
Turn on flashlight.
turnOff() -> Promise
Turn off flashlight.
subscribe(eventName, callback) -> unsubscribe
Subscribing to event. Use the onTurn
event to subscribe to a state change turnState
.
FAQ
react-native-lantern: NativeModule.ReactNativeLantern is null
.
When running Jest tests, an error occurs This is due to the fact that when running Jest tests, there is no native implementation of the module. You need to add a mock file:
// __mocks__/react-native-lantern.ts
const lanternMockModule = {
ready: jest.fn(() => Promise.resolve()),
turn: jest.fn(() => Promise.resolve()),
turnOn: jest.fn(() => Promise.resolve()),
turnOff: jest.fn(() => Promise.resolve()),
subscribe: jest.fn(() => () => {}),
};
export default lanternMockModule;