[英]Ho to pass react-router-dom link userid through props?
I have six components which I want to redirect to six different links我有六个组件要重定向到六个不同的链接
import Post from "../post/Post";
import ".//posts.css";
export default function Posts() {
return (
<div className="flex-container">
<Post title="2BHK 2Bath" rent="20000" redirect="1" />
<Post title="3BHK 2Bath" rent="25000" redirect="2" />
<Post title="1BHK 1Bath" rent="14000" redirect="3" />
<Post title="3BHK 3Bath" rent="30000" redirect="4" />
<Post title="2BHK 2Bath" rent="18000" redirect="5" />
<Post title="1BHK 1Bath" rent="10000" redirect="6" />
</div>
);
}
Post.jsx Post.jsx
export default function Post({ title, rent, redirect }) {
return (
<div className="post">
<img
className="postImg"
src="https://www.thespruce.com/thmb/aGEhef5NbpY6R_Fahn5fIW8SAHk=/941x0/filters:no_upscale():max_bytes(150000):strip_icc():format(webp)/put-together-a-perfect-guest-room-1976987-hero-223e3e8f697e4b13b62ad4fe898d492d.jpg"
alt=""
/>
<div className="postInfo">
<div className="postCats"></div>
<span className="postTitle">
<Link
to={{ pathname: "/post/{redirect}" }}
className="link"
>
{title}
</Link>
</span>
<hr />
<span className="postDate">Rent: {rent}/-</span>
</div>
</div>
);
}
This does not work, clicking on the first post with redirect: 1
redirects me to this URL http://localhost:3000/post/%7Bredirect%7D
这不起作用,单击带有
redirect: 1
将我重定向到此 URL http://localhost:3000/post/%7Bredirect%7D
I also tried making it like this from a stackoverflow answer我也尝试从stackoverflow答案中做到这一点
<Link
to={{ pathname: "/post", state: { redirect } }}
className="link"
>
{title}
</Link>
But this also did not work.但这也没有用。
How I can make so that, I can pass the redirect_id through the props of the components.我如何做到这一点,我可以通过组件的道具传递redirect_id。
Example:例子:
When I click on component of <Post title="2BHK 2Bath" rent="20000" redirect="1" />
it should redirect me to http://localhost:3000/post/1
当我单击
<Post title="2BHK 2Bath" rent="20000" redirect="1" />
的组件时,它应该将我重定向到http://localhost:3000/post/1
How can I do this?我怎样才能做到这一点?
you can try using template string您可以尝试使用模板字符串
<Link to={{ pathname: `/post/${redirect || 0}` }} className="link">
{title}
</Link>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.