React-router: Add default params?

Created on 26 Apr 2015  路  5Comments  路  Source: ReactTraining/react-router

It seems currently there's no way to have default query params. The use case is the following:

this.context.router.transitionTo('users', null, {page: 1})

Currently it generates a path similar to /users?page=1. However, it would be nice to specify page=1 being the default and so the generated path woudl just be /users.

Since you already have the nice <Router/> syntax for configuration, I think we can extend it like below:

<Router name="users" defaultParams={{page: 1}} />

Frank

Most helpful comment

@ryanflorence "For default params on a route just have a params default prop on your route component"

How would I go about doing this? Thanks in advance.

All 5 comments

+1 for default route query params

+1 here too, should also work with redirects..

UseCase:

<Redirect 
  from='old/route/that/contains/just/one/:parameter'
  params={{newRouteParameter:1, secondNewRouteParameter:2}}
  to='newRouteThatHasMoreThenOneParameter'} />

right now this does not work, I expected the parameters to be combined (like Object.assign()).

+1

For default params on a route just have a params default prop on your route component

If we used Object.assign() for redirects, how would you opt-out of the merge if you wanted to replace?

In 1.0 you have onEnter at the route level, so you could do whatever logic you want to perform a redirect :)

@ryanflorence "For default params on a route just have a params default prop on your route component"

How would I go about doing this? Thanks in advance.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davetgreen picture davetgreen  路  3Comments

misterwilliam picture misterwilliam  路  3Comments

ryansobol picture ryansobol  路  3Comments

sarbbottam picture sarbbottam  路  3Comments

yormi picture yormi  路  3Comments