JSPM

  • Created
  • Published
  • Downloads 36
  • Score
    100M100P100Q39906F
  • License MIT

Flashlight support on React Native

Package Exports

  • react-native-lantern

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-lantern) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-native-lantern

Flashlight support on React Native

Warning!!! Support only Android (>= API 23 (>= Android 6.0))

Getting started

npm i react-native-lantern

Mostly automatic installation

npx react-native link react-native-lantern (use npx react-native unlink react-native-lantern to uninstall)

Manual installation

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.reactnative.lantern.ReactNativeLanternPackage; to the imports at the top of the file
  1. 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')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
    compile project(':react-native-lantern')

Usage

import React, { useState, useEffect, useCallback } from 'react';
import { View, Button } from 'react-native';
import lantern from 'react-native-lantern';

const Main = () => {
    const [isDisabled, setDisabled] = useState(true);
    const [isTurnOn, setTurnState] = useState(false);

    useEffect(() => {
        // call on change turn state (fire on subscribe, return current turn state)
        const unsubscribe = lantern.subscribe('onTurn', (event) => setTurnState(event.value));
        return unsubscribe;
    }, []);

    useEffect(() => {
        (async () => {
            // initialize module
            await lantern.ready();
            setDisabled(false);
        })();
    }, []);

    const onPress = useCallback(async () => {
        if (isTurnOn) {
            await lantern.turnOff();
        } else {
            await lantern.turnOn();
        }
        // or `await lantern.turn(!isTurnOn)`
    }, [isTurnOn]);

    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.

License

MIT