【发布时间】:2020-02-10 12:12:05
【问题描述】:
在 React 中,我们在重新加载页面时使用 useEffect 获取数据:
useEffect(() => {
let ignore = false
const fetchingData = async () => {
const res = await fetch(<your_path>)
const data = await res.json()
if (!ignore) setData(data)
};
fetchingData()
return () => { ignore = true }
}, [])
但是如何在 Next.js 中做到这一点?当页面重新加载时,Fetch 不会在 getInitialProps 中触发。
【问题讨论】:
-
你的问题就是我的答案,谢谢。
-
@FatemehQasemkhani,我的问题是基于我在 next.js 中的无能,后来我意识到 getInitialProps 中 axios 请求的问题。很高兴为您提供帮助!:)
标签: javascript html reactjs fetch next.js