我正在開發本機應用程式,在 App.js 中,在我的 App 函式之前,我在 AsyncStorage 中獲取我的令牌,并在 App 函式中檢查此令牌是否存在或他是否未過期。但是要獲取 AsyncStorage 中的資訊,請使用 Async 函式,而我的 App 函式不能是異步的。App 函式稍后會被 react native 呼叫,所以我不能使用“then”和回呼。
let token;
let expirationDate;
AsyncStorage.getItem('accessToken').then((response) => token = response)
AsyncStorage.getItem('expirationTokenDate').then((response) => expirationDate = response)
const App = () => {
if (!token | !expirationDate | expirationDate < Date.now()) {
//return something
}
//return another thing
}
export default App;
uj5u.com熱心網友回復:
將您的變數轉換為狀態,即:
const [token, setToken] = useState(null);
const [expirationTokenDate, setExpirationTokenDate] = useState(null);
AsyncStorage.getItem('accessToken').then((response) => setToken(response.data)
AsyncStorage.getItem('expirationTokenDate').then((response) => setExpirationTokenDate(response.data)
const App = () => {
if (!token | !expirationDate | expirationDate < Date.now()) {
//return something
}
//return another thing
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334689.html
標籤:javascript 反应原生 异步
上一篇:如何使用VirtualBox和Ubuntu作為開發環境和Windows10使用AndroidStudio作為Android模擬器在ReactNative中進行開發
