您好我试图通过React Router的链接组件将Props传递给Details
组件。我不想在页面上显示Detail
组件,它应该在单击按钮时呈现,但网址也应该如此' / 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>
<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;
答案 0 :(得分:11)
如果想让你的新路线像/ 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.state
或this.props.history.location.state
答案 1 :(得分:1)
在你的链接代码中应该是这个,我相信:
object MyFormats extends DefaultJsonProtocol with FamilyFormats {
// gives a slight performance boost, but isn't necessary
implicit val MyAdtFormats = shapeless.cachedImplicit[JsonFormat[TestT]]
}
import MyFormats._
路线与您目前拥有的路线相同。