【发布时间】:2023-03-06 19:25:01
【问题描述】:
我正在尝试在 Next.js 环境中使用 SWR。
const Best = (props: InferGetStaticPropsType<typeof getStaticProps>) => {
const { data } = useSWR('/best', apis.getBestProduct, {
initialData: props.initialData,
});
console.log(data);
return (
...SOME PRESENTER
);
};
export const getStaticProps: GetStaticProps = async () => {
const data = await apis.getBestProduct();
return { props: { initialData: data } };
};
export default Best;
我想将useSWR 与getStaticProps 一起使用。
但是这段代码会出现这样的错误。
Server Error
Error: Error serializing `.initialData.config.transformRequest[0]` returned from `getStaticProps` in "/best".
Reason: `function` cannot be serialized as JSON. Please only return JSON serializable data types.
数据来得好,不知道为什么不行。
我使用的是 MacOS,自定义 Node.js 服务器,这是 localhost。
【问题讨论】:
标签: reactjs serialization next.js