【发布时间】:2021-03-22 17:29:11
【问题描述】:
我创建了这个有一些嵌套路由的反应组件。问题是嵌套页面组件要么根本不渲染,尽管 URL 发生了变化,要么只是返回一个空白页面。
我尝试了其他帖子的建议,例如在父路由中添加/删除exact,但它仍然无法正常工作。
以下是我的代码:
// the parent component
<div className="App">
<Router>
<Navbar />
<Switch>
<Route exact path="/" render={() => <MyPage accessToken={accessToken} />}/>
<Route path="/editing/:playlistId" render={(props) =>
<EditingPlaylist {...props} accessToken={accessToken} />} />
</Switch>
</Router>
</div>
//the child component EditingPlaylist
render() {
const { pathname } = this.props.location;
return (
<div className="editing">
<Switch>
<Route exact path={pathname} render={() =>
<Searchbar accessToken={this.props.accessToken} />} />
<Route path={`${pathname}/test`} component={<p>Test</p>} />
<Route path={`${pathname}/album/:albumId`} render={
(props) => <AlbumPage {...props} accessToken={this.state.accessToken} />} />
<Route path={`${pathname}/artist/:artistId`} render={
(props) => <ArtistProfile {...props} accessToken={this.state.accessToken} />} />
</Switch>
</div>)
}
export default withRouter(EditingPlaylist);
【问题讨论】:
-
EditingPlaylist组件的pathname的值是多少?你想渲染哪个网址? -
试试
this.props.match.url而不是pathname
标签: javascript reactjs react-router react-router-dom