扫描二维码后打开浏览器- React Native

Browser opens after scanning QR code- ReactNative

我正在使用 react-native-camera 模块扫描二维码。但是在扫描之后它总是打开浏览器并尝试访问 qr 值作为 url。如何停止这个浏览器打开的东西。我只需要获得 qr 值..任何帮助。这是我的代码。

_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>);
            }

    }

您的问题可能与注释行有关。尝试提取该行。如果没有任何效果,您可以尝试另一个模块。你可以使用 'react-native-qrcode-scanner' 这样会更容易。这是 github link.

假设您希望在成功扫描条码后重定向到其他屏幕。据观察,条形码扫描后可能无法获得二维码数据。因此,必须处理是否成功检索到 qr 值。为此,您需要按如下方式更新“_handleBarCodeRead”代码:

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);
}