gpt4 book ai didi

javascript - 在 React-Router 中使用 Link 传递 Prop

转载 作者:行者123 更新时间:2023-11-30 07:01:54 24 4
gpt4 key购买 nike

你好,我正在尝试使用来自 React Router 的链接组件将 Props 传递给 Details 组件。我不想在页面上显示 Detail 组件,它应该在单击按钮时呈现,但当新组件呈现时,url 应该看起来像这样 '/details/KvhNJecsqr6JFMSRTS'。

 class  Card extends Component {
render(props){
return(
<Col xs={12} md={4}>
<Thumbnail src="./someiamge">
<h3>{this.props.cardHeading}</h3>
<p>{this.props.cardDesc}</p>
<Button bsStyle="primary">Mieten</Button>&nbsp;
<Button bsStyle="default"><Link to='/details' params={{cardId: this.props.cardId}} 'here i wanna pass some props how i do this ?' >Details</Link></Button>
</Thumbnail>
</Col>

)
}
}

export default Card

这是我的路由器的东西

<BrowserRouter>
<div>
<Route name='details' path='/details/:cardId' component={Details}/>
</div>
</div>
</BrowserRouter>

hier 是我的 Details 组件:

    class Details extends Component {
render() {
return (
<div >
<div style={{textAlign: "left"}}>
<h3>{this.props.cardHeading}</h3>
<p>{this.props.cardDesc}</p>
.......
</div>
</div>
);
}
}

export default Details;

最佳答案

如果你想让你的新路线像/details/:cardId,那么我想这应该足够了:

<Link to={`/details/${this.props.cardId}`} />

但是,如果你想添加一些额外的属性,那么根据 documentation ,您可以在 location.state 中传递它们:

<Link to={{
pathname: `/details/${this.props.cardId}`,
state: {
cardHeading: 'This is a heading',
cardDesc: 'Description'
}
}}/>

然后,为了在您的组件中访问此状态,您可以使用 this.props.location.statethis.props.history.location.state

关于javascript - 在 React-Router 中使用 Link 传递 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47287855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com