我正在制作一个离线--第一个带有function的应用程序--本机、世博和redux,我需要根据互联网连接将用户更改发送回服务器,所以我使用expo -后台-fetch定期完成这项任务,使用自定义函数,或者使用netInfo检查互联网连接作为完成工作的条件,但是它不起作用,因为它总是说“错误:无效的钩子调用。钩子只能在一个功能组件的体内调用”,无论是在还原器中还是在自定义函数中。
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文件上,然后在组件上,我用等待registerBackgroundFetchAsync()注册了任务;我的sync.js文件如下所示:
import {useNetInfo} from "@react-native-community/netinfo";
export function sync () {
const netInfo = useNetInfo();
}有什么解决办法或解决办法吗?
发布于 2022-06-03 18:08:30
看起来,您不应该在sync回调中调用defineTask。useNetInfo是指从function函数组件中调用的。您可以使用其他不是钩子的方法。https://github.com/react-native-netinfo/react-native-netinfo#fetch可能是你的朋友。
https://stackoverflow.com/questions/72484471
复制相似问题