[英]React router v4 - Rendering two components on same route
I have these routes 我有这些路线
<Route exact path={`/admin/caters/:id`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
When I navigate to the first route I get a cater with a given ID. 当我导航到第一条路线时,将获得具有给定ID的餐饮服务。 And the Cater component is rendered
餐饮组件被渲染
When I navigate to the second route, the CreateCater component is rendered on the page, but I noticed that some redux actions that are used in the Cater component are being run. 当我导航到第二条路线时,页面上将呈现CreateCater组件,但是我注意到正在运行Cater组件中使用的某些redux操作。 So both component are somehow being rendered - but I can't figure out why.
所以这两个组件都以某种方式被渲染-但是我不知道为什么。
Here are the components: 以下是组件:
Cater: 迎合:
class Cater extends Component {
async componentDidMount() {
console.log('Cater component did mount')
const { match: { params: { id }}} = this.props
this.props.get(id)
}
render() {
const { cater } = this.props
if(!cater) {
return null
}
else {
return (
<div>
... component data ...
</div>
)
}
}
}
const mapStateToProps = (state, props) => {
const { match: { params: { id }}} = props
return {
cater: caterSelectors.get(state, id)
}
}
const mapDispatchToProps = (dispatch, props) => {
return {
get: (id) => dispatch(caterActions.get(id))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cater)
CreateCater: CreateCater:
export default class CreateCaterPage extends Component {
render() {
return (
<React.Fragment>
<Breadcrumbs />
<CaterForm />
</React.Fragment>
)
}
}
When I go to /admin/caters/create' I can see the console.log in the componenDidMount() lifecycle method inside the Cater component. 当我转到/ admin / caters / create'时,可以在Cater组件内的componenDidMount()生命周期方法中看到console.log。
I cant figure out what I am doing wrong :( 我不知道我在做什么错了:(
/create
matches /:id
, so it makes sense that this route matches. /create
匹配/:id
,因此该路由匹配是有意义的。 I recommend forcing :id
to look for numeric only: 我建议强制
:id
只查找数字:
<Route exact path={`/admin/caters/:id(\\d+)`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
Likewise, you can follow @jabsatz's recommendation, use a switch, and have it match the first route that matches. 同样,您可以遵循@jabsatz的建议,使用开关,并使其与匹配的第一条路线匹配。 In this case, you would need to ensure that the
/admin/caters/create
route is the first <Route />
element matched. 在这种情况下,您需要确保
/admin/caters/create
路由是第一个匹配的<Route />
元素。
The problem is that :id
is matching with create
(so, it thinks "see cater with id create
"). 问题在于
:id
与create
匹配(因此,它认为“请参见id为create
饮食”)。 The way to solve this is to put the wildcard matching route last, and wrapping all the <Routes/>
with a <Switch/>
, so it only renders the first hit. 解决此问题的方法是将通配符匹配路由放在最后,并用
<Switch/>
包裹所有<Routes/>
<Switch/>
,因此它仅呈现第一个匹配项。
Check out the docs if you have any more questions: https://reacttraining.com/react-router/core/api/Switch 如果您还有其他问题,请查看文档: https : //reacttraining.com/react-router/core/api/Switch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.