Answer the question
In order to leave comments, you need to log in
How to solve a banal routing task in a React application?
Good afternoon, 3 days in react, before that a few years in the front + 2 years with EmberJS. I broke my brain with routing in react.
There is a news feed along the way - 'social/news'. It contains a list of news with a link to a single news item of the form 'social/news/8'. The task is banal when switching to show the full text of the news. How to organize routing?
This is what the root App component looks like:
class App extends React.Component {
public render() {
return (
<Router>
<div className="news-wrapper">
<Navbar/>
<Route exact={true} path={'/social/news'} component={News}/>
<Route exact={true} path={'/social/news/:id'} component={Newspage}/>
</div>
</Router>
);
}
}
export default App;
class Newsitem extends React.Component<MyProps> {
public render() {
return (
<Router>
<div key={this.props.news.id}>
<img src={this.props.news.big_image} alt=""/>
<Link to={`/social/news/${this.props.news.id}`}>{this.props.news.title}</Link>
<hr/>
</div>
</Router>
);
}
}
Answer the question
In order to leave comments, you need to log in
The Router component should be one and it is usually taken out to the entry point.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question