当我刷新 NextJS 动态路由页面时,ID 消失了

Luc*_*omb 7 reactjs next.js

我正在尝试让动态路由工作,以便使用 url 中的 id 显示和检索正确的信息。当我访问该页面时,这有效,但当我重新加载页面时,ID 为空。这可以修复吗?我在互联网上找不到答案。

任何帮助,将不胜感激。

参考代码

//Initializing router
const router = useRouter();

//Getting id from url
const { id } = router.query;

//Fetching postdata
  const getPostData = async () => {
    setLoading(true);
    await db
      .collection("Posts")
      .doc(id)
      .get()
      .then(function (doc) {
        if (doc.exists) {
          setPostData(doc.data());
          setLoading(false);
        } else {
          //router.push("/404");
        }
      })
      .catch(function (error) {
        //router.push("/404");
      });
  };
Run Code Online (Sandbox Code Playgroud)

Mih*_*aru 9

这是有关 Next.js 路由的已知警告,您可以在此处阅读。

无论如何,将其添加到使用 router.query 的页面上:

export async function getServerSideProps(context) {
    return {
        props: {},
    };
}
Run Code Online (Sandbox Code Playgroud)