javascript 浏览屏幕后,Expo条码扫描器停止工作

pcww981p  于 2023-11-15  发布在  Java
关注(0)|答案(2)|浏览(91)

所以,我面临着一个问题,当我导航到我的扫描仪屏幕,并返回到前一个屏幕,然后再次导航到我的扫描仪屏幕,条形码扫描仪不工作.甚至控制台日志不工作.我必须清除现金和所有数据从博览会应用程序,以便再次工作扫描仪屏幕.我真的不知道是什么原因造成的问题,但高度怀疑导航.有人能帮助我请?我添加我的扫描仪屏幕右下方。

import React, { useState, useEffect } from "react";
import {
  Text,
  View,
  FlatList,
  Button,
  Modal,
  Pressable,
  Alert,
  StyleSheet,
} from "react-native";
import { BarCodeScanner } from "expo-barcode-scanner";
import axios from "axios";
import { localIP, ngrokServer } from "../constants";
import allStyles from "../components/molecules/Styles";

const styles = allStyles;

export default function ScannerScreen({ navigation }) {
  const [hasPermission, setHasPermission] = useState(null);
  const [scanned, setScanned] = useState(false);

  useEffect(() => {
    setReset(false);
  });

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

  const handleBarCodeScanned = async ({ type, data }) => {
    setScanned(true);
    console.log("Data: ", data);
  };

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

  return (
    <View style={styles.scannerScreenContainer}>
      <BarCodeScanner
        onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
        style={StyleSheet.absoluteFillObject}
      />
      {scanned && reset && (
        <Button title={"Tap to Scan Again"} onPress={() => setScanned(false)} />
      )}
      
    </View>
  );
}

字符串
我正在使用axios.post,并认为这可能是问题的原因,但当我删除该代码块并再次运行时,它不扫描QR码。

xkrw2x1b

xkrw2x1b1#

我也有同样的问题,我通过添加焦点事件(当屏幕聚焦时发出)的侦听器来修复它。
它看起来是这样的:

useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
      // do something - for example: reset states, ask for camera permission
      setScanned(false);
      setHasPermission(false);
      (async () => {
      const { status } = await BarCodeScanner.requestPermissionsAsync();
      setHasPermission(status === "granted"); 
      })();
    });

    // Return the function to unsubscribe from the event so it gets removed on unmount
    return unsubscribe;
  }, [navigation]);

字符串
试着使用这个代码,看看它是否有效。
来源:https://reactnavigation.org/docs/navigation-events/#navigationaddlistener

knsnq2tg

knsnq2tg2#

在世博会上遇到了同样的问题,用Miztory的解决方案解决了。我只是不得不

import { useNavigation } from 'expo-router';

字符串

const navigation = useNavigation()


剩下的都一样。谢谢你@Miztory

相关问题