react-native-torch

Please provide the following:

  1. SDK Version: 36.0.0
  2. Platforms(Android/iOS/web/all): all

Hello Everyone,
I am new to both react native and expo and I am trying to create an app that utilizes the flashlight. I have tried to test the react-native-torch library, however I cannot get it to work. When I try to use Torch.switchState(true) or Torch.switchState(false), I get an Unhandled Promise Rejection(id: 0):
TypeError: null is not an object (evaluating “Torch.switchStatte”)…

The code can be seen below: (App.js):
import React, { useState, useEffect } from ‘react’;

import { Button, StyleSheet, Text, View } from ‘react-native’;

import { Camera } from ‘expo-camera’;

import Torch from ‘react-native-torch’;

export default function App() {

const [ hasPermission, setHasPermission ] = useState(null);

useEffect(() => {

(async () => {

  const { status } = await Camera.requestPermissionsAsync();

  setHasPermission(status === 'granted');

})();

}, );

if (hasPermission === null) {

return <View />;

}

if (hasPermission === false){

return <Text>No access to camera</Text>;

}

return (

<View style={styles.container}>

  <Text>Open up App.js to start working on your app!</Text>

  <Button

    title = "Open Torch"

    onPress={()=>Torch.switchState(true)}

  />

  <Button

    title = "Close Torch"

    onPress={()=>Torch.switchState(false)}

  />      

</View>

);

}

const styles = StyleSheet.create({

container: {

flex: 1,

backgroundColor: '#fff',

alignItems: 'center',

justifyContent: 'center',

},

});

The package.json:
{

“main”: “node_modules/expo/AppEntry.js”,

“scripts”: {

"start": "expo start",

"android": "expo start --android",

"ios": "expo start --ios",

"web": "expo start --web",

"eject": "expo eject"

},

“dependencies”: {

"expo": "~36.0.0",

"expo-camera": "~8.0.0",

"react": "~16.9.0",

"react-dom": "~16.9.0",

"react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",

"react-native-torch": "^1.1.5",

"react-native-web": "~0.11.7"

},

“devDependencies”: {

"@babel/core": "^7.0.0",

"babel-preset-expo": "~8.0.0"

},

“private”: true

}

Hope you can help.
Thank you very much.

I’m also very curious to know!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.