0

I’m using react-native-camera module to scan a qr code. But after scanning that it always opens the browser and try to access qr value as url. How to stop this browser opening thing. I just needed to get the qr value.. any help. Here is my code.

_handleBarCodeRead(e) {

        try {
            Vibration.vibrate();
            this.setState({scanning: false});
            this.setState({qrcode:e.data});
            //Linking.openURL(e.data).catch(err => console.error(‘An error occured’, err));
            console.log(e.data);

            const {navigate} = this.props.navigation;
            navigate(‘TransactionVerified’);

            //return;
        } catch (error) {
            console.log(error);
        }
    }
    getInitialState() {
        return {
            scanning: true,
            cameraType: Camera.constants.Type.back
        }
    }

    render(){
        if(this.state.scanning) {
            return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                Scan Code
                </Text>
                <View style={styles.rectangleContainer}>
                <Camera
                 style={styles.camera}
                 type={this.state.cameraType}
                 onBarCodeRead={this._handleBarCodeRead.bind(this)}
                 barCodeTypes={[Camera.constants.BarCodeType.qr]}
                 >
                    <View style={styles.rectangleContainer}>
                    <View style={styles.rectangle}/>
                    </View>
                </Camera>
                </View>
                {/* <Text style={styles.instructions}>
                Double tap R on your keyboard to reload,{‘\n’}
                </Text> */}
            </View>
            );
            }
            else{
            return (<View  style={styles.container}>
                <Text style={styles.welcome}>
                Scan Code
                </Text>
                {/* <Text style={styles.instructions}>
                Double tap R on your keyboard to reload,{‘\n’}
                </Text>      */}
            </View>);
            }

    }
Rizwan
  • 2,369
  • 22
  • 27

2 Answers2

2

Assuming you are expecting redirect to other screen if bar-code get scanned successfully. It has been observed that, after bar-code scan one may not get qr data. Hence it is essential to handle whether successfully retrieved the qr value or not. For this you need to update your '_handleBarCodeRead' code as follows:

try {
    if (e.data !== undefined) {
        Vibration.vibrate();
        this.setState({scanning: false});
        this.setState({qrcode:e.data});
        //Linking.openURL(e.data).catch(err => console.error(‘An error occured’, err));
        console.log(e.data);

        const {navigate} = this.props.navigation;
        navigate(‘TransactionVerified’);
    }
} catch (error) {
    console.log(error);
}
Sandy.....
  • 2,833
  • 2
  • 15
  • 26
0

Your problem probably related with comment line. Try with extracting that line. If nothing works you might try another module. you can use 'react-native-qrcode-scanner' that will be more easier. here is the github link.

Berkay Kaan
  • 295
  • 3
  • 10