首页 > 解决方案 > 哪种方式从父组件获取信息更快更合适?

问题描述

我正在使用 Next.js 开发一个应用程序。

我想通过 Child 组件中的 useRouter 获取查询信息。哪种方式获取查询信息更快更合适?

  1. 从 ParentComponent 将 props 传递给每个 ChildComponent
const ParentComponent = () => {
  const router = useRouter()
  const { query } = router

  return (
    <>
      <ChildComponent query={query} />
      <ChildComponent query={query} />
    </>
  )
}

const ChildComponent = ({ query }) => {
  console.log(query)

  return (
    <></>
  )
}
  1. 在每个 ChildComponent 中获取查询
const ParentComponent = () => {
  return (
    <>
      <ChildComponent />
      <ChildComponent />
    </>
  )
}

const ChildComponent = () => {
  const router = useRouter()
  const { query } = router

  console.log(query)

  return (
    <></>
  )
}

标签: reactjsnext.js

解决方案


推荐阅读