npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-lantern

v1.0.3

Published

Flashlight support on React Native

Downloads

19

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

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.

FAQ

When running Jest tests, an error occurs react-native-lantern: NativeModule.ReactNativeLantern is null.

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;

License

MIT