Estoy intentando hacer un Lector de Barra
con un componente de expo
la cual funciona bastante bien, el problema surge cuando quiero pasar los datos capturado
a otro componente, no llega ni se actualiza.
https://docs.expo.io/versions/latest/sdk/bar-code-scanner/
import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { BarCodeScanner } from 'expo-barcode-scanner';
import Codigo from './Codigo';
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);
var barra='...';
useEffect(() => {
(async () => {
const { status } = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
const handleBarCodeScanned = ({ type, data }) => {
setScanned(true);
barra = { tipo: type, codigo: data };
alert(`Bar code with type ${type} and data ${data} has been scanned!`);
console.log(barra);
};
if (hasPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View
style={{
flex: 2,
flexDirection: 'column',
justifyContent: 'flex-end',
}}>
<Codigo data={barra}/>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
{scanned && (
<Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />
)}
</View>
);
}
import React, { Component } from 'react';
import { Text, View } from 'react-native';
export default class Codigo extends Component {
constructor(props){
super(props);
this.state = {
connected: false,
barra: this.props.data
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.data !== this.props.barra) {
console.log(nextProps.data);
}
}
render(){
return(
<View><Text>{this.props.data}</Text></View>
)
}
}
En la primera parte, captura y asigna normal, pero en el segundo componente llega el valor inicial que le di ...