如何捕获父捕获React Rouyter动态参数?
我希望能够将孩子的id
存储在父组件中。但是,我不确定该怎么做。我知道我可以在孩子中调用useParams()
,然后将id
传递给父母,但这似乎是一种完成此操作的回旋方式。
<Route path="/project/:id" element={<Child />}>
</Route>
const childId = ???
I want to be able to store the child's id
in the parent component. However, I'm not sure how to do so. I know that I can call useParams()
in the child, and then pass the id
back to the parent, but this seems like a roundabout way to accomplish this.
<Route path="/project/:id" element={<Child />}>
</Route>
const childId = ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在父组件中创建一个状态,然后在接收ID之后,使用USEPARAMS()将SetState发送到子女组件中,并在子组件中调用SetState;
父母的
孩子
You can create a state in a parent component, then send setState to the child and call setState in child component, after receiving id, using useParams();
Parent
Child