Run an asynchronous function in React-Nactivate only the first time it is called

Asked

Viewed 30 times

2

I am using the function of the expo-camera Camera component

onBarCodeScanned={(event) => {
      handleQRCode(event);
}}

And every time there’s a bar code he calls me handleQRCode, but just passing the Qrcode in front of the camera it already calls this function more than once, (usually more than 3 times with a very fast stride), but inside my handleQRCode I call an API, and like the handleQRCode is called often happens many requests the API, already tested put boolean variables, both "normal" both using the useState for while the requisition is taking place he does not make new requisitions, but does not work, I do not know what else to do, someone can help me?

Code of handleQRCode:

const [visibleModalBuscando, setVisibleModalBuscando] = useState(false);

async function handleQRCode(event) {
    if (!visibleModalBuscando) {
      setVisibleModalBuscando(true);
      let result = await buscarDadosParaHome(event.data);
      if (result) {
        navigation.navigate("Home");
      } else {
        Alert.alert(
          "QRCode inválido",
          "Esse QRCode não é válido!\n Tente novamente! Se o problema persistir contate o responsável!"
        );
      }
      setVisibleModalBuscando(false);
    }
  }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.