问题是,如果我删除初始值,那么一切正常。我认为具有初始值的数据不会被缓存。但我不知道如何解决这个问题。如果初始数据变异返回未定义。
在页面组件中:
const { data, error } = useSWR("/api/company/", {
initialData: company,
});
export const getServerSideProps: GetServerSideProps<IManage> = async (ctx) => {
ensureAuth(ctx);
let company: ICompany[] | null = null;
await instanceWithSSR(ctx) // axios config
.get(`/api/company/`)
.then((response) => {
company = response?.data;
})
.catch((error) => {
console.log(error);
});
return {
props: {
company: company || null,
},
};
};
我的突变:
mutate(url, async (company: ICompany[]) => {
console.log(company)
if (company) {
return [...company, companyItem];
}
}, false);