reactjs - 哪种方式从父组件获取信息更快更合适?
问题描述
我正在使用 Next.js 开发一个应用程序。
我想通过 Child 组件中的 useRouter 获取查询信息。哪种方式获取查询信息更快更合适?
- 从 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 (
<></>
)
}
- 在每个 ChildComponent 中获取查询
const ParentComponent = () => {
return (
<>
<ChildComponent />
<ChildComponent />
</>
)
}
const ChildComponent = () => {
const router = useRouter()
const { query } = router
console.log(query)
return (
<></>
)
}
解决方案
推荐阅读
- docker - 如何创建正确的 Dockerfile
- mongodb - Mongodb如何获取$type“对象”
- kubernetes - “请求的地址无效”试图从 K8S 外部的客户端访问 K8S 中的 Consul 服务器
- node.js - 如何限制显示的对象数量?
- go - 使用 sendgrid 发送 ssl 电子邮件而未完成
- sql-server - 如何从日期中选择MM
- r - 如何使用 aws.s3 包函数(write_using 和 read_using)从 EC2 上的 R 访问 S3 数据?
- debugging - Visual Studio 2017 中的“启用 GPU 反汇编集合”在哪里?
- java - 什么可以在不更改代码的情况下破坏 android 构建?
- java - 在 Java 中使用“oracle.xdb.XMLType”