Javascript 有没有一种方法可以在React Router中将嵌套路由ID用作prop参数
所以基本上我有一个画廊项目数组,其中包含类“GalleryItem”对象(包含画廊名称和图像及其名称的列表) 我还有一个名为“Gallery”的组件,它将GalleryItem类对象作为道具并渲染它 我想做的是使用…/galleryName/:galleryName导航到单个页面中呈现特定的库 画廊渲染良好,但我需要这与嵌套的路线一起工作Javascript 有没有一种方法可以在React Router中将嵌套路由ID用作prop参数,javascript,reactjs,routes,nested,react-router,Javascript,Reactjs,Routes,Nested,React Router,所以基本上我有一个画廊项目数组,其中包含类“GalleryItem”对象(包含画廊名称和图像及其名称的列表) 我还有一个名为“Gallery”的组件,它将GalleryItem类对象作为道具并渲染它 我想做的是使用…/galleryName/:galleryName导航到单个页面中呈现特定的库 画廊渲染良好,但我需要这与嵌套的路线一起工作 <Switch> <Route path="/galleries/:name" render={(props) => <Gall
<Switch>
<Route path="/galleries/:name" render={(props) => <Gallery {...props} galleryItem={this.state.galleryItems[:name]} />} />
<Switch>
} />
显然,这不起作用,所以我在问它是如何完成的,以及如何知道我是否完全错了。路由组件:
- 匹配
- 位置
- 历史
<Switch>
<Route
path="/galleries/:name"
render={props => (
<Gallery
{...props}
galleryItem={this.state.galleryItems[props.match.params.name]}
/>
)}
/>
<Switch>
(
)}
/>
道具匹配将包含有关路线匹配方式的信息。您可以这样做:
<Route
path="/galleries/:name"
exact
render={props =>
<Gallery {...props} galleryItem={galleryItems(props.match.path.split(':')[1])} />
}
/>
}
/>
您可以在Gallery组件中获得匹配
表单道具。