Home > Net >  React Native : Async and Await is a reserved word error inside onPress
React Native : Async and Await is a reserved word error inside onPress

Time:04-21

I'm facing a weird problem. In my react native app I have a Googlebutton which trigger onPress and it will check the condition within it, but the problem is I have await reserved word which returns me an error says Unexpected reserved word await I'm trying to apply Pincode

Main Code

 import PINCode, {hasUserSetPinCode,resetPinCodeInternalStates,deleteUserPinCode,
 } from "@haskkor/react-native-pincode";

<GoogleSigninButton
   style={{ width: 252, height: 58 }}
   size={GoogleSigninButton.Size.Wide}
   color={GoogleSigninButton.Color.Dark}
   onPress={() => {

   const hasPin = await hasUserSetPinCode();
   if (fingerprint === true) {
       googleLogin();
   }

   else if (hasPin) {
       console.log("Alert pinnn should pop up");
   }

   else {
      console.log("Alert should pop up");
      setModalVisible(true);
    }
   }
 }
/>

This is what I've tried I've put async (hasUserSetPinCode) before the await but it returns no value in my console, it seems it's not working

putting an async

import PINCode, {hasUserSetPinCode,resetPinCodeInternalStates,deleteUserPinCode,
}from "@haskkor/react-native-pincode";

<GoogleSigninButton
     style={{ width: 252, height: 58 }}
     size={GoogleSigninButton.Size.Wide}
     color={GoogleSigninButton.Color.Dark}
     onPress={async() => {
     /* ------------------ The async below Doesn't work ----------------------------------- */
     
     const hasPin = await hasUserSetPinCode();
        if (fingerprint === true) {
           googleLogin();
        }
        else if (hasPin) {
           console.log("Alert pinnn should pop up");
        }
        else {
           console.log("Alert should pop up");
           setModalVisible(true);
        }
     }
  
/>

enter image description here

CodePudding user response:

Place async before the function in which it is used.

import PINCode, {hasUserSetPinCode,resetPinCodeInternalStates,deleteUserPinCode,
 } from "@haskkor/react-native-pincode";

<GoogleSigninButton
   style={{ width: 252, height: 58 }}
   size={GoogleSigninButton.Size.Wide}
   color={GoogleSigninButton.Color.Dark}
   onPress={async () => {

   const hasPin = await hasUserSetPinCode();
   if (fingerprint === true) {
       googleLogin();
   }

   else if (hasPin) {
       console.log("Alert pinnn should pop up");
   }

   else {
      console.log("Alert should pop up");
      setModalVisible(true);
    }
   }
 }
/>
  • Related