在Next.js中通过页面发送数据的问题

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

Issue sending data through pages in Next.js

问题

from my main page, I want, when clicking a button, to go to another page and transfer data to it, but it throws an error "TypeError: Cannot read properties of undefined (reading 'users')". There are a lot of conflicting information about this on the Internet, so I thought it would be better to ask here. Here are the two components:

    return (
      <>
        <h1>Zeke's Bill Splitter</h1>

        <h2>Enter the amount of people</h2>

        <form onSubmit={checkAmount}>
          <input 
          type="number"
          onChange={saveInput} />
          </form>

        <Link 
        href={{
          pathname: '/pages/mainPage',
          query: { users: users }
        }}>Go to main</Link>
      </>
    );
  };

  export default Page;

And this is the component that should receive the data, but apparently it's not receiving it:

  import React from 'react'
  import { useRouter } from "next/navigation"

  const MainPage = () => {

    const router = useRouter()
    const { userAmount } = router.query.users;

    return (
      <div>{userAmount}</div>
    )
  }

  export default MainPage

Does anybody have any idea? This is my first Next.js project and it's pretty confusing.

英文:

from my main page, I want, when clicking a button, to go to another page and transfer data to it, but it throws an error "TypeError: Cannot read properties of undefined (reading 'users')". There are a lot of conflicting information about this on the Internet, so I thought it would be better to ask here. Here are the two components:

    return (
      &lt;&gt;
        &lt;h1&gt;Zeke&#180;s Bill Splitter&lt;/h1&gt;

        &lt;h2&gt;Enter the amount of people&lt;/h2&gt;

        &lt;form onSubmit={checkAmount}&gt;
          &lt;input 
          type=&quot;number&quot;
          onChange={saveInput} /&gt;
          &lt;/form&gt;

        &lt;Link 
        href={{
          pathname: &#39;/pages/mainPage&#39;,
          query: { users: users }
        }}&gt;Go to main&lt;/Link&gt;
      &lt;/&gt;
    );
  };

  export default Page;

And this is the component that should recieve the data, but aparently it´s not recieving it:

  &quot;use client&quot;
  import React from &#39;react&#39;
  import { useRouter } from &quot;next/navigation&quot;

  const MainPage = () =&gt; {

    const router = useRouter()
    const { userAmount } = router.query.users;

    return (
      &lt;div&gt;{userAmount}&lt;/div&gt;
    )
  }

  export default MainPage

Does anybody have any idea? This is my first Next.js project and it's pretty confusing.

答案1

得分: 1

After Next 13我们有两个useRouter,一个是从&#39;next/router&#39;导出的,应该与旧页面路由一起使用;另一个是从&#39;next/navigation&#39;导出的,应该与新应用程序路由一起使用。router.query属性仅存在于&#39;next/router&#39;导出的路由中,因此如果您使用来自&#39;next/navigation&#39;(应用程序文件夹实现)的路由,router.query将返回undefined。使用应用程序路由获取查询参数时,您需要使用useSearchParams。您可以在此处阅读更多信息:https://nextjs.org/docs/app/api-reference/functions/use-search-params

"use client"
import React from &#39;react&#39;
import { useSearchParams } from &#39;next/navigation&#39;;

const MainPage = () =&gt; {

  const searchParams = useSearchParams();
  const userAmount = searchParams.get(&#39;users&#39;);

  return (
    &lt;div&gt;{userAmount}&lt;/div&gt;
  )
}

export default MainPage
英文:

After Next 13 we have two useRouter, one is exported from &#39;next/router&#39; that should be used with the old pages router and another exported from &#39;next/navigation&#39; that should be used with the new app router. The router.query property only exists in the router from &#39;next/router&#39;, since you are using the one from &#39;next/navigation&#39; (app folder implementation) router.query is returning as undefined. With the app router in order to get the query params you need to use useSearchParams. You can read more about it here https://nextjs.org/docs/app/api-reference/functions/use-search-params

&quot;use client&quot;
import React from &#39;react&#39;
import { useSearchParams } from &#39;next/navigation&#39;;

const MainPage = () =&gt; {

  const searchParams = useSearchParams();
  const userAmount = searchParams.get(&#39;users&#39;);

  return (
    &lt;div&gt;{userAmount}&lt;/div&gt;
  )
}

export default MainPage

huangapple
  • 本文由 发表于 2023年5月17日 22:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273434.html
匿名

发表评论

匿名网友

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

确定