Javascript React+中的路由状态名称;重演
我有两条路由使用相同的组件(表单),一条用于创建新条目,另一条用于编辑。来自React路由器的状态名称,我不能只检查this.route.name==='edit'=>fetch(itemAPI) 一个选项是编写reducer/action在我的redux应用商店中创建“编辑”状态,但我想知道这是否是最佳实践,是否有更简单的方法来检查我在应用程序中的位置(哪条路径) 我的路线:Javascript React+中的路由状态名称;重演,javascript,reactjs,react-router,redux,Javascript,Reactjs,React Router,Redux,我有两条路由使用相同的组件(表单),一条用于创建新条目,另一条用于编辑。来自React路由器的状态名称,我不能只检查this.route.name==='edit'=>fetch(itemAPI) 一个选项是编写reducer/action在我的redux应用商店中创建“编辑”状态,但我想知道这是否是最佳实践,是否有更简单的方法来检查我在应用程序中的位置(哪条路径) 我的路线: <Route component={ItemNew} path='/items/edit/:id' />
<Route component={ItemNew} path='/items/edit/:id' />
<Route component={ItemNew} path='/items/new' />
用于在当前URL上执行匹配的最后一部分的
路由组件显示在this.props.Route
;参与匹配的嵌套Route
s的完整列表位于this.props.routes
数组中。您可以将任意道具传递到这些Route
s上,然后检索它们。例如:
<Route component={ItemNew} name="edit" path='/items/edit/:id' />
<Route component={ItemNew} name="add" path='/items/new' />
@默认情况下,alexndm不是。或者使用钩子(比如onEnter
或者componentDidMount
)将其推入状态,或者查看类似或的内容
<Route component={ItemNew} name="edit" path='/items/edit/:id' />
<Route component={ItemNew} name="add" path='/items/new' />
componentDidMount () {
if(this.props.route.name === 'edit') {
this.props.fetchItem() //dispatch API action
}
}