Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

In Next.js how can I pass data to a dynamic route if 'route.query' is not an available option?

I'm working on an embedded app, the main URL doesn't change when I go to my dynamic url page, and router.query keeps returning undefined. When I go to my dynamic route I see my 'hello' so I know it's working, but other than that I'm stumped on how to proceed. My best guess at the moment is to use a span tag instead of a Link tag with an onClick function that not only redirects to the dynamic route, but also sends the path of the dynamic route to a context API so I can retrieve it inside the dynamic route.

Here is my Link code:

        href={{
          pathname: `/customer/[query]`,
          query: { query: `${item.ownerEmail}` },
        }}
      >

And this is the code in the dynamic page: 

import { useRouter } from "next/router";

function Customer(props) {
  const router = useRouter;
  console.log(router.query);
  return <div>Hello</div>;
}
question from:https://stackoverflow.com/questions/65847700/in-next-js-how-can-i-pass-data-to-a-dynamic-route-if-route-query-is-not-an-ava

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to initialize the hook. Should look like this

 const router = useRouter();

Let me know if that solved the problem


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...