Поскольку вы упомянули react-router | react-router-dom
в v4
, вы можете сделать это одним из следующих способов:
A
- Заменить на
<Link
className="btn btn-primary" // some bootstrap class
to={{
pathname: "/watch",
state: { posterId: poster.id } // I am assuming post has id
}}
/>
- в
WatchComponent
class WatchComponent extends React.Component {
componentDidMount() {
const { posterId } = this.props.location.state;
//make ajax request to the server and get poster details
//promise resolve, set response into state
// this.setState({ poster: res.json()})
}
render() {
const { poster } = this.state;
return (
// render poster details here.
)
}
}
или
Вы можете просто сделать это
<Link
className="btn btn-primary" // some bootstrap class
to={`/watch/${posterId}`} // provided that Route of WatchComponent is defined as (URL parameter) <Route path="watch/:posterId" component={WatchComponent} />
/>
затем в WatchComponent вы делаете
class WatchComponent extends React.Component {
componentDidMount() {
const { posterId } = this.props.macth.params;
//make ajax request to the server and get poster details
//promise resolve, set response into state
// this.setState({ poster: res.json()})
}
render() {
const { poster } = this.state;
return (
// render poster details here.
)
}
}