Skip to content

Instantly share code, notes, and snippets.

@weeravit
Last active March 12, 2018 16:55
Show Gist options
  • Select an option

  • Save weeravit/1172734db5f8952546aa631be4631cf3 to your computer and use it in GitHub Desktop.

Select an option

Save weeravit/1172734db5f8952546aa631be4631cf3 to your computer and use it in GitHub Desktop.
import React from 'react';
import {Alert, StyleSheet, Text, View} from 'react-native';
import {BarCodeScanner, Permissions} from 'expo';
export default class App extends React.Component {
state = {
hasCameraPermission: null,
isQRScanable: true
}
async componentWillMount() {
const {status} = await Permissions.askAsync(Permissions.CAMERA);
this.setState({hasCameraPermission: status === 'granted'});
}
render() {
const {hasCameraPermission, isQRScanable} = this.state;
if (hasCameraPermission === null) {
return <Text>Requesting for camera permission</Text>;
} else if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
} else if (!isQRScanable) {
return (
<View style={{flex: 1}}>
<View style={{flex: 0.2, justifyContent: 'center', alignItems: 'center'}}>
<Text style={{fontSize: 25}}>HELLO QR SCANNER</Text>
</View>
<View style={{flex: 0.6, backgroundColor: '#000000'}}/>
<View style={{flex: 0.2}}/>
</View>
);
} else {
return (
<View style={{flex: 1}}>
<View style={{flex: 0.2, justifyContent: 'center', alignItems: 'center'}}>
<Text style={{fontSize: 25}}>HELLO QR SCANNER</Text>
</View>
<View style={{flex: 0.6}}>
<BarCodeScanner
onBarCodeRead={this._handleBarCodeRead}
style={StyleSheet.absoluteFill}
/>
</View>
<View style={{flex: 0.2}}/>
</View>
);
}
}
_handleBarCodeRead = ({type, data}) => {
// หลังจากที่มันอ่าน data ได้ ให้ handle api ตรงนี้ต่อเลย
this.disableQRScanner()
Alert.alert(
'Alert Title',
data,
[
{text: 'OK', onPress: () => this.enableQRScanner()},
],
{cancelable: false}
)
}
disableQRScanner() {
this.setState({
...this.state,
isQRScanable: false
})
}
enableQRScanner() {
this.setState({
...this.state,
isQRScanable: true
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment