reactjs - NextJS getStaticProps 没有被调用

标签 reactjs next.js apollo-client

我试图让 getStaticProps 返回一些数据,但该函数从未被调用。我查阅了官方文档和示例,还检查了 SO 的各种答案,但我仍然无法让它工作。

事实上,getStaticProps 中的 console.log 从未运行,这让我认为我的代码存在配置错误,但我找不到问题的根本原因。

我的apollo客户端配置如下:

apollo client configuration

具有 ApolloClient 选项的 constants 文件如下

constants file with apollo configuration

我的组件如下,在useEffect中,console.log命令记录undefined,但我从未看到控制台。来自 gerStaticProps 内部的 log 命令被调用。

Main component

我还有一个自定义 _app 组件,如下所示:

custom _app component

我的文件夹结构如下:

folder structure

我认为问题很可能出在我的 _app.tsx 组件中,并且 this page from the official documentation包含解决方案,但即使在尝试之后我也没有解决我的问题。

最佳答案

根据提供的信息,两个可能的原因是,

  1. getStaticProps 必须在页面中使用。检查您使用的文件是否是页面而不是组件。
  2. 文件结构可能导致该问题。关注此主题Is this Next.JS folder structure recommended?

关于reactjs - NextJS getStaticProps 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69248001/

相关文章:

javascript - 如何在react js中插入多维数组数据?

javascript - ReactJS:为什么我的 div 没有出现?

reactjs - Jest 类型错误 : is not a constructor in Jest. 模拟

reactjs - Nextjs API POST 请求正文未被解析

graphql - Apollo 客户端片段未嵌入数据

javascript - 如何防止内部div的事件处理程序在外​​部div的事件处理程序中执行?

javascript - 如何在 next.js 应用程序中从服务器端获取客户端的 IP 地址?

next.js - 如何安装 Next.js 的 Semantic ui

graphql - Apollo 客户端 : Can @defer be used with client side resolvers?

graphql - 订阅不适用于 apollo-client 和 hot-chocolate .net core Web api