每当我单击按钮调用同步时,我都会得到:错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能由于以下原因之一而发生:
- 你可能有不匹配的 React 版本和渲染器(例如 React DOM)
- 您可能违反了 Hooks 规则
- 您可能在同一个应用程序中拥有多个 React 副本有关如何调试和修复此问题的提示,请参阅https://reactjs.org/link/invalid-hook-call 。
有任何想法吗?
import { synchronize } from '@nozbe/watermelondb/sync';
import { database } from '../../index';
const SYNC_API_URL = 'https://example.com/sync';
export async function sync() {
await synchronize({
database,
pullChanges: async ({lastPulledAt}) => {
const response = await fetch(SYNC_API_URL, {
method: 'GET',
headers: {'Authorization': 'Bearer' + $(user.token)},
body: JSON.stringify({lastPulledAt}),
});
if (!response.ok) {
throw new Error(await response.text());
}
const {changes, timestamp} = await response.json();
return {changes, timestamp};
},
pushChanges: async ({changes, lastPulledAt}) => {
const response = await fetch(
`${SYNC_API_URL}?lastPulledAt=${lastPulledAt}`,
{
method: 'POST',
headers: {'Authorization': 'Bearer' + $(user.token)},
body: JSON.stringify(changes),
},
);
if (!response.ok) {
throw new Error(await response.text());
}
},
});
}