【发布时间】:2016-04-06 21:40:03
【问题描述】:
我有 2 条使用相同组件(表单)的路线,一条用于创建新条目,另一条用于编辑。来自 React Router 的状态名称 has been removed,我不能只检查 this.route.name === 'edit' => fetch(itemAPI)。
一种选择是编写 reducer/action 以在我的 redux 商店中创建“编辑”状态,但我想知道这是否是最佳实践,以及是否有更简单的方法来检查我在应用程序中的位置(哪个路由) .
我的路线:
<Route component={ItemNew} path='/items/edit/:id' />
<Route component={ItemNew} path='/items/new' />
在我的 ItemNew 组件中,我想:
componentDidMount () {
let stateName = this.props.location.state
if(stateName === 'edit') {
this.props.fetchItem() // dispatch API action
}
}
【问题讨论】:
标签: javascript reactjs react-router redux