Problem with expo-barcode-scanner

How i can disable expo barcode scanner with button and enable

Any one can help me

If you want so show what the camera sees on the screen, but just enable/disable the barcode scanning, you can store the state of your button and then enable/disable the onBarCodeScanned function. Like this example.

Otherwise you can show/hide the BarCodeScanner depending on the state of your button.

1 Like

Hello @wodin
thanks for your answer but i do it and not working

this is my code


import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { BarCodeScanner } from 'expo-barcode-scanner';

export default function App() {
  const [hasPermission, setHasPermission] = useState(null);
  const [scanned, setScanned] = useState(false);
  const [done,setDone] = useState(false);

  useEffect(() => {
    (async () => {
      const { status } = await BarCodeScanner.requestPermissionsAsync();
      setHasPermission(status === 'granted');
    })();
  }, []);

  const handleBarCodeScanned = ({ type, data }) => {
    setScanned(true);
    alert(`Bar code with type ${type} and data ${data} has been scanned!`);
    setDone(true);
  };

  if (hasPermission === null) {
    return <Text>Requesting for camera permission</Text>;
  }
  if (hasPermission === false) {
    return <Text>No access to camera</Text>;
  }

  	return (done) ? (<View><Text>Ok scanned</Text>) : 
 
    (<View
      style={{
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'flex-end',
      }}>
      <BarCodeScanner
        onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
        style={StyleSheet.absoluteFillObject}
      />

      {scanned && <Button title={'Close Scanned'} onPress={() => setDone(false)} />}
    </View>
  );
}

Could you please elaborate on what you mean by “it’s not working”?

What do you do (step by step)?
What do you expect to happen?
What happens instead?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.