Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React+中的路由状态名称;重演_Javascript_Reactjs_React Router_Redux - Fatal编程技术网

Javascript React+中的路由状态名称;重演

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' />

我有两条路由使用相同的组件(表单),一条用于创建新条目,另一条用于编辑。来自React路由器的状态名称,我不能只检查this.route.name==='edit'=>fetch(itemAPI)

一个选项是编写reducer/action在我的redux应用商店中创建“编辑”状态,但我想知道这是否是最佳实践,是否有更简单的方法来检查我在应用程序中的位置(哪条路径)

我的路线:

<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
  }
}