'react-native-torch is not working properly
I am trying to use react-native-torch with react native. I took this code and put it into my project. I have also installed react-native-torch with npm install --save react-native-torch. My App.js looks like this (sorry for using code snippet, I wasn't able to achieve propriate formating with this formating):
import React, { Component } from 'react';
import {
AppRegistry,
Button,
NativeModules,
StyleSheet,
Text,
View
} from 'react-native';
import Torch from 'react-native-torch';
export default class TorchDemo extends Component {
constructor(props) {
super(props);
this.state = {
isTorchOn: false,
};
}
_handlePress() {
const { isTorchOn } = this.state;
Torch.switchState(!isTorchOn);
this.setState({ isTorchOn: !isTorchOn });
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
RCTTorch Demo
</Text>
<Button
onPress={this._handlePress.bind(this)}
title="Toggle Torch"
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('TorchDemo', () => TorchDemo);
Everything runs as it should, but when I tap the toogle torch button, I am getting this warning and the flashlight will not turn on:
I have tried many forms of their demo codes, but the problem was always with Torch.switchState.
Does anyone knows how to fix this issue?
Thank you very much for any help.
Jan
Solution 1:[1]
It looks like you're using a managed workflow, it won't work through Expo Go. You need to use a development client for native code to work. Create it with EAS.
- Install the expo-dev-client library
expo install expo-dev-client - Create an eas.json
eas build:configure - Build client and install on device/emulator
eas build -p android --profile development - Use
expo start --dev-clientinstead of 'expo start'
Solution 2:[2]
Did you try building the app again as this module requires linking? If not, you need to run the React Native app again.
For android run: npx react-native run-android
For ios run: npx react-native run-ios
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitro_ |
| Solution 2 |

