我们有什么办法可以在getStaticProps()中使用自定义钩子。这样做的原因是,我们使用 Contentful CMS 从Delivery API获取数据,并且我们更容易使用自定义挂钩来获取某些数据。
现在,当我们调用getStaticProps()函数中的useHomePageData时,我们在尝试使用简单的对象破坏来获取数据时会收到错误。
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Run Code Online (Sandbox Code Playgroud)
这是getStaticProps()中的代码
export function getStaticProps() {
const { data, isPending, error } = useFetchPagesProps('phasePage');
const { name, title, slug, sections } = data;
return {
props: {
name,
title,
slug,
sections
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这个错误意味着什么,但仍然有人知道例如任何npm 库或类似的东西,以便我们可以使用这个自定义钩子?
PS:我们还在自定义挂钩中使用useEffect(),因此这可能会导致我认为的问题
getStaticProps
技术上与 React 无关。本质上,这只是一个对 Next.js 框架本身具有特殊含义和功能的函数。所以不,你不能在那里使用钩子(至少不能setState
useEffect
在其中使用等的钩子)。
归档时间: |
|
查看次数: |
5703 次 |
最近记录: |