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
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
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.
expo install expo-dev-clienteas build:configureeas build -p android --profile developmentexpo start --dev-client instead of 'expo start'