``` import React, { Component, } from 'react' import { View, StyleSheet, Alert, } from 'react-native' import Barcode from 'react-native-smart-barcode' import TimerEnhance from 'react-native-smart-timer-enhance' class BarcodeTest extends Component { constructor(props) { super(props); this.state = { viewAppear: false, }; } render() { return ( <View style={{flex: 1, backgroundColor: 'black',}}> {this.state.viewAppear ? <Barcode style={{flex: 1, }} ref={ component => this._barCode = component } onBarCodeRead={this._onBarCodeRead}/> : null} </View> ) } componentDidMount() { let viewAppearCallBack = (event) => { this.setTimeout( () => { this.setState({ viewAppear: true, }) }, 255) } this._listeners = [ //this.props.navigator.navigationContext.addListener('didfocus', viewAppearCallBack) ] } componentWillUnmount () { // this._listeners && this._listeners.forEach(listener => listener.remove()); } _onBarCodeRead = (e) => { console.log(`e.nativeEvent.data.type = ${e.nativeEvent.data.type}, e.nativeEvent.data.code = ${e.nativeEvent.data.code}`) this._stopScan() Alert.alert(e.nativeEvent.data.type, e.nativeEvent.data.code, [ {text: 'OK', onPress: () => this._startScan()}, ]) } _startScan = (e) => { this._barCode.startScan() } _stopScan = (e) => { this._barCode.stopScan() } } export default TimerEnhance(BarcodeTest) ``` My code above. On the app just show a black screen. PS: I'm not using navigation, but rn router flux
Activity
KesavSundarM commentedon Jul 4, 2018
I had similar issue, Add the following lines in AndroidManifest.xml to give permission to access camera. This fixed.
Also replace
this._listeners = [ //this.props.navigator.navigationContext.addListener('didfocus', viewAppearCallBack) ]With
this.timer = setTimeout( () => { this.setState({ viewAppear: true, }) }, 255)Also add
this.timer && clearTimeout(this.timer);in the componentWillUnmount ()
sajithnwijerathne commentedon Aug 28, 2018
@rodrigofbm , @KesavSundarM , Hi , You should give permissions from device settings, or you can use react-native-permissions.
bigkrys commentedon Jun 20, 2019
i did it ,but i does not work,it still balck screen