反应本机QR扫描仪不扫描

问题描述:

我想在本机构建一个QR扫描仪android。我有以下代码,但它不会扫描任何东西。代码中需要什么才能使其工作?反应本机QR扫描仪不扫描

import React, { Component } from 'react'; 
 

 
import { 
 
    AppRegistry, 
 
    StyleSheet, 
 
    View, 
 
    Text, 
 
    TouchableHighlight, 
 
    TouchableOpacity, 
 
    Image, 
 
    Button 
 
} from 'react-native'; 
 
import BarcodeScanner from 'react-native-barcodescanner'; 
 

 
export default class test extends React.Component { 
 
    constructor(props) { 
 
    super(props); 
 

 
    this.state = { 
 
     torchMode: 'off', 
 
     cameraType: 'back', 
 
    }; 
 
    } 
 

 
    barcodeReceived(e) { 
 
    console.log('Barcode: ' + e.data); 
 
    console.log('Type: ' + e.type); 
 
    } 
 

 
    render() { 
 
    return (
 
     <View style={{flex: 1, flexDirection: 'row'}}> 
 
     <BarcodeScanner 
 
     onBarCodeRead={this.barcodeReceived} 
 
     style={{ flex: 1 }} 
 
     torchMode={this.state.torchMode} 
 
     cameraType={this.state.cameraType} 
 
     /> 
 
     </View> 
 
    ); 
 
    } 
 
} 
 

 

 
AppRegistry.registerComponent('test',() => test);

How it looks on phone

+0

在你的构造函数中添加'this.barcodeReceived = this.barcodeReceived.bind(this)',或者改变你的函数声明来使用箭头函数,例如:'barcodeReceived =(e)=> {}'。这与React中的this有关。没有绑定或使用箭头函数进行词法范围界定,'this'是'undefined'。 – Dan

使用,这样就可以斌的功能

onBarCodeRead={this.barcodeReceived.bind(this)} 

可能这可以帮助你!