2017-08-01 104 views
0

我想在本機構建一個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

回答

0

使用,這樣就可以斌的功能

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

可能這可以幫助你!