我正在使用 react-native、expo 和 redux 制作一個離線優先的應用程式,我需要根據互聯網連接將用戶更改發送回服務器,所以我使用 expo-background-fetch 來完成這項任務定期使用自定義函式或調度一些使用 netInfo 來檢查互聯網連接作為完成作業的條件的操作,但它不起作用,因為總是說“錯誤:無效的掛鉤呼叫。掛鉤只能在內部呼叫函陣列件的主體”在 reducer 或自定義函式中。
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
import {sync} from './sync';
const BACKGROUND_FETCH_TASK = 'background-fetch';
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
const now = Date.now();
console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`);
sync();
// Be sure to return the successful result type!
return BackgroundFetch.BackgroundFetchResult.NewData;
});
async function registerBackgroundFetchAsync() {
console.log('registered');
return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 1, // 15 minutes
stopOnTerminate: false, // android only,
startOnBoot: true, // android only
});
}
這是在全域范圍內的 App.js 檔案上,然后在我使用 await registerBackgroundFetchAsync() 注冊任務的組件上;我的 sync.js 檔案如下所示:
import {useNetInfo} from "@react-native-community/netinfo";
export function sync () {
const netInfo = useNetInfo();
}
任何解決方案或解決方法?
uj5u.com熱心網友回復:
看起來你不應該sync在你的defineTask回呼中呼叫。useNetInfo旨在從 React 函陣列件中呼叫。您可以使用不是鉤子的其他方法。https://github.com/react-native-netinfo/react-native-netinfo#fetch可能是你的朋友。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486065.html
標籤:反应式 还原 反应本机社区网络信息
