4

I have an icon of flashlight in my react-native application. I want to hide it if mobile device does not support flashlight or not has flashlight.

In native android we have this solution:

[https://stackoverflow.com/a/19599365/6644676][1]

Furqan
  • 787
  • 2
  • 13
  • 28

1 Answers1

0

use this npm package.

https://www.npmjs.com/package/react-native-torch

if the device won't have torch than the below code will throw exception and catch block will be executed and thus you will get to know whether the device has torch or not

try {
   await Torch.switchState(newTorchState);
   this.setState({ isTorchOn: newTorchState });
} 
catch (e) {
  ToastAndroid.show(
    'We seem to have an issue accessing your torch',
    ToastAndroid.SHORT
  );
}
Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23
  • Thanks for your answer. But it seems a workaround. Not a proper solution. – Furqan Nov 06 '19 at 07:19
  • 3
    then i believe you need to create a native module in your react native project using the android native code and access it into react native component, ref: https://facebook.github.io/react-native/docs/native-modules-android – Angad Tiwari Nov 06 '19 at 07:24
  • @AngadTiwari Can `react-native-torch` be used to determine if the flashlight is ON ? – sj_959 Oct 21 '21 at 08:08