![](/img/trans.png)
[英]Navigating in Async function - React Navigation - React Native
[英]Async Function not recognize in React Native
我知道這很常見,但我遇到了一個奇怪的問題。 在我的本機反應應用程序中,我只想在文件中調用 function 但它似乎返回錯誤,在我的functionOne
下這將返回functionTwo
但它給了我一個錯誤。 需要幫忙
[ReferenceError: Can't find variable: functionTwo]
更新: functionOne
指示重置 PinCode,然后注銷 googleSignIn,因為它具有functionTwo();
在functionOne
和functionTwo
內部表示注銷 googleSignIn
我將這些函數放在<AuthContext.Provider>
中,因為我想像全局函數/變量一樣在屏幕上的任何地方重用或調用 function
更新: 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 >
);
更新了這是我最近嘗試過的,但它也返回了錯誤
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 >
);
試試這個方法
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;
您可以將聲明函數的方式更改為
async function functionOne(){}
另一個也一樣。 至少這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.