如何通过参数将参数与React中的导航函数路由?
有没有办法将某些参数传递给带有导航功能的路由是React?我找到了以下方法,但是它不起作用,因为第二个文件中的路由参数不确定。
import { useNavigate } from 'react-router-dom'
const exploreTopic = () =>{
navigate(`/topic/${props.id}`,{id:props.id});
};
return(
<div onClick={exploreTopic}>smth</div>
)
import { useParams } from 'react-router-dom'
import './style.css'
const SingleTopic = ({route,navigate}) => {
return (
<div>
{route.params.id}
</div>
)
}
export default SingleTopic
Is there a way to pass some parameters to a route with the navigate function is react? I found the below approach, but it doesn't work since the route parameter in the second file is undefined.
import { useNavigate } from 'react-router-dom'
const exploreTopic = () =>{
navigate(`/topic/${props.id}`,{id:props.id});
};
return(
<div onClick={exploreTopic}>smth</div>
)
import { useParams } from 'react-router-dom'
import './style.css'
const SingleTopic = ({route,navigate}) => {
return (
<div>
{route.params.id}
</div>
)
}
export default SingleTopic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以以这种方式传递数据
,而您的单元将成为
You can pass the data this way
And your SingleTopic will become
使用
USELOCATION
:您可以从
singletopic
component中删除elements{路由}
:use
useLocation
:you can remove the elements
{route,navigate}
from theSingleTopic
component: