如何使用’next/navigation’中的useRouter将一个对象传递到另一个页面?

huangapple go评论143阅读模式
英文:

How to pass an object to another page using the useRouter from 'next/navigation'?

问题

我在一页上,想要将用户重定向到另一页。
我想使用来自next/navigationuseRouter钩子来向新页面发送一个对象,因为我正在使用应用程序目录。如何更改下面的代码以传递一个对象?

  1. import { useRouter } from 'next/navigation'
  2. const router = useRouter()
  3. router.push('/dashboard')

如果我使用页面目录,我会这样做:

  1. import { useRouter } from 'next/router'
  2. const router = useRouter()
  3. router.push({
  4. pathname: '/dashboard',
  5. query: { key: value },
  6. })

我在文档中没有找到如何实现这一点。

英文:

I am on one page, and I want to redirect the user to another page.
I would like to send an object to the new page using the hook useRouter from next/navigation because I'm using the app directory. How can I change the code below to pass an object?

  1. import { useRouter } from 'next/navigation'
  2. const router = useRouter()
  3. router.push('/dashboard')

If I were using the page directory, I would do it like this:

  1. import { useRouter } from 'next/router'
  2. const router = useRouter()
  3. router.push({
  4. pathname: '/dashboard',
  5. query: {key : value},
  6. })

I didn't find how to do it in the docs.

答案1

得分: 1

app文件夹中,router.push(href: string)仅接受href作为字符串参数。如果您需要查询字符串,您需要自行设置,就像这个示例中所示。

在您的情况下,可以这样做:

  1. "use client";
  2. import { useRouter } from "next/navigation";
  3. export default function ExampleClientComponent() {
  4. const router = useRouter();
  5. const createQueryString = (name, value) => {
  6. const params = new URLSearchParams();
  7. params.set(name, value);
  8. return params.toString();
  9. };
  10. return (
  11. <button
  12. onClick={() => {
  13. router.push("/dashboard" + "?" + createQueryString("key", "value"));
  14. }}
  15. >
  16. Redirect with a query parameter
  17. </button>
  18. );
  19. }

您可以通过传递给页面组件的searchParams来读取它:

  1. // app/dashboard/page.js
  2. export default function Page({ searchParams }) {
  3. return <div>{searchParams.key}</div>;
  4. }

或者在任何客户端组件中使用useSearchParams函数:

  1. "use client";
  2. import { useSearchParams } from "next/navigation";
  3. export default function Page() {
  4. const searchParams = useSearchParams();
  5. return <div>{searchParams.get("key")}</div>;
  6. }

⚠️:查询参数是字符串。您可以一次传递多个参数。但如果您需要将对象传递给一个键,您需要使用params.set("key", JSON.stringify(value))

在使用它的另一侧使用之前,请调用JSON.parse对值进行解析。

英文:

In the app folder, router.push(href: string) accepts as a parameter only the href as a string. If you need query strings, you need to set them yourself, as they show in this example.

In your case, it can be like so:

  1. &quot;use client&quot;;
  2. import { useRouter } from &quot;next/navigation&quot;;
  3. export default function ExampleClientComponent() {
  4. const router = useRouter();
  5. const createQueryString = (name, value) =&gt; {
  6. const params = new URLSearchParams();
  7. params.set(name, value);
  8. return params.toString();
  9. };
  10. return (
  11. &lt;button
  12. onClick={() =&gt; {
  13. router.push(&quot;/dashboard&quot; + &quot;?&quot; + createQueryString(&quot;key&quot;, &quot;value&quot;));
  14. }}
  15. &gt;
  16. Redirect with a query parameter
  17. &lt;/button&gt;
  18. );
  19. }

You would read it with the searchParams passed to your page component:

  1. // app/dashboard/page.js
  2. export default function Page({ searchParams }) {
  3. return &lt;div&gt;{searchParams.key}&lt;/div&gt;;
  4. }

Or using the useSearchParams function in any client component:

  1. &quot;use client&quot;;
  2. import { useSearchParams } from &quot;next/navigation&quot;;
  3. export default function Page() {
  4. const searchParams = useSearchParams();
  5. return &lt;div&gt;{searchParams.get(&quot;key&quot;)}&lt;/div&gt;;
  6. }

⚠️: query parameters are stings. You can pass multiple at once. But if you need to pass an object to one key, you need to params.set(&quot;key&quot;, JSON.stringify(value)).
And call JSON.parse on the value before using it on the other side.

huangapple
  • 本文由 发表于 2023年8月10日 20:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定