Reputation: 43
I would like create ONE Route path for form add and edit. That is posible? If yes how. If no, please let me know what is the best practice. I must define route similar like that:
< Route path="/edituser/:id" component={EditUser} />
< Route path="/edituser" component={EditUser} />
Upvotes: 1
Views: 2367
Reputation: 1978
If you read the react-router documentation you will see that you can pass nullable params to the router.
it means that you can check for a param, then if its null then its an Add situation and if not its a edit situation.
in your case you should define you route like this :
<Route path="/edituser/:id?" component={EditUser} />
which is the id is nullable and can be pass or not pass.
and then in the constructor
or ComponentDidMount
check it like this:
if (typeof this.props.match.params.id === 'undefined')
//Add
else
//Edit
Good luck.
Upvotes: 4