React-router: Default params for a route

Created on 21 Aug 2016  路  1Comment  路  Source: ReactTraining/react-router

I am missing an option to pass default params on to a route. It was requested a couple of times (#683, #1126), but the responses do not really solve the issue (defining a default prop "params" on the route component does not work).

The use case in my case:

<Route path="users" component={Users} title="User management">
    <Route path="0" title="Create user" />
    <Route path=":id" title="Edit user" />
</Route>

To be able to differ from create- and edit-operations within the route component, it would help a lot if i get a params.id = 0 within the "Create user" route component.

Proposal I:
Writing the default params within the path.

<Route path="users" component={Users} title="User management">
    <Route path=":id=0" title="Create user" />
    <Route path=":id" title="Edit user" />
</Route>

Proposal II:
Adding a prop "params" on routes. Params would be merged and populated to this.props.params of the route component. Params from the url would have precedence.

<Route path="users" component={Users} title="User management">
    <Route path="0" params={{ id: 0 }} title="Create user" />
    <Route path=":id" title="Edit user" />
</Route>

Most helpful comment

Just do something like

const { id = 0 } = props.params;

There's no particular need for it to be supported at the core library level.

>All comments

Just do something like

const { id = 0 } = props.params;

There's no particular need for it to be supported at the core library level.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ackvf picture ackvf  路  3Comments

ArthurRougier picture ArthurRougier  路  3Comments

ryansobol picture ryansobol  路  3Comments

stnwk picture stnwk  路  3Comments

misterwilliam picture misterwilliam  路  3Comments