在 React Native 中无法识别异步函数

Async Function not recognize in React Native

我知道这很常见,但我遇到了一个奇怪的问题。在我的本机反应应用程序中,我只想在文件中调用一个函数,但它似乎 returns 一个错误,在我的 functionOne 下,这将 returns functionTwo 但它给出我一个错误。需要帮助

[ReferenceError: Can't find variable: functionTwo]

更新:functionOne 表示重置 PinCode,然后注销 googleSignIn,因为它在 functionOne 中有 functionTwo();functionTwo 表示注销 googleSignIn

我将这些函数放在 <AuthContext.Provider> 中,因为我想在屏幕上随处重复使用或调用该函数,就像全局函数一样 function/variable

更新:AuthProvider.js

return (
    <AuthContext.Provider
      value={{
        user,
        setUser,
        functionOne: async () => {
          try {
            await deleteUserPinCode();
            await resetPinCodeInternalStates();
            functionTwo();   //this returns an error --------------------------------
          } catch (e) {
            console.log(e);
            console.log("Please contact administrator");
          }
        },

        functionTwo: async () => {
          try {
            GoogleSignin.configure({});
            await GoogleSignin.revokeAccess();
            await GoogleSignin.signOut();
            setUser({ user: null });
            AsyncStorage.setItem('userPrivilege', '');
            await auth().signOut();
            console.log("sa try");
          } catch (e) {
            console.log(e);
            console.log("Please contact administrator");
          }
        },
      }
      } >
      {children}
    </AuthContext.Provider >
  );

更新了这是我最近尝试过的方法,但它 returns 我也是错误的

Can't find variable:functionOne

  functionOne = async (e) => {
    try {
      await deleteUserPinCode();
      await resetPinCodeInternalStates();
      functionTwo();
    } catch (e) {
      console.log(e);
      console.log("Please contact administrator");
    }
  };
  return (
    <AuthContext.Provider
      value={{
        user,
        setUser,
        functionOne: this.functionOne,
        
        functionTwo: async () => {
          try {
            GoogleSignin.configure({});
            await GoogleSignin.revokeAccess();
            await GoogleSignin.signOut();
            setUser({ user: null });
            AsyncStorage.setItem('userPrivilege', '');
            await auth().signOut();
            console.log("sa try");
          } catch (e) {
            console.log(e);
            console.log("Please contact administrator");
          }
        },
      }
      } >
      {children}
    </AuthContext.Provider >
  );

您可以将声明函数的方式更改为

async function functionOne(){}

另一个也一样。至少对我有用。

试试这个方法

class AppProvider extends React.Component {
  
  const functionOne = async (e) => {
    try {
      ....
    } catch (e) {
      ....
    }
  };
  
  const functionTwo = async (e) => {
    try {
      ....
    } catch (e) {
      ....
    }
  };

  render() {
    return (
      <AppContext.Provider
        value={{
          ....
          functionOne: this.functionOne,
          functionTwo: this.functionTwo,
        }}
      >
     {children}
     </AppContext.Provider>
    );
  }
}
export default AppProvider;