I have 2 routes using the same component (form), one for creating a new entry, and the other for editing. The state name has been removed from the React Router, I can’t just check if this.route.name === 'edit' => fetch (itemAPI).
One option is to write a reducer / action to create an “edit” state in my redux store, but I was wondering if this is the best practice, and if there is an easier way to check where I am in the application (which route),
My routes:
<Route component={ItemNew} path='/items/edit/:id' />
<Route component={ItemNew} path='/items/new' />
In my ItemNew component, I would like to:
componentDidMount () {
let stateName = this.props.location.state
if(stateName === 'edit') {
this.props.fetchItem()
}
}
source
share