React-router: Default params on routes?

Created on 13 Jan 2015  路  5Comments  路  Source: ReactTraining/react-router

Couldn't find this anywhere, but it seems common enough as a use case that I must have just missed it?

          <Route path="/:user/:param=defaultvalue" />

or

          <Route path="/:user/:param" defaultParams={{param: "defaultvalue"}} />

Most helpful comment

It seems really backwards to me that in order to set a default value for a param, we need to create a new throw-away React component, load that one, transition to a new route, and then load the actual component we want.

It seems as if it would be much more straightforward to be able to have some simple syntax to define what we want a param to be if it's not specified in the url. That way we can simply render the desired data right away, without having to render throw-away components. Something like OP's original solution would be ideal.

All 5 comments

var RedirectToDefaultValue = React.createClass({
  willTransitionTo (transition, params) {
    transition.redirect(`/${params.user}/defaultValue`);
  },
  render () { return null; }
});

<Route path="/:user">
  <DefaultRoute handler={RedirectToDefaultValue}/>
  <Route path="/:user/:param" />
</Route>

:+1: Useful. Might be helpful to have in docs?

Very useful, thanks!

Correct me if I'm wrong, but shouldn't it be

var RedirectToDefaultValue = React.createClass({
  statics: {
    willTransitionTo (transition, params) {
      transition.redirect(`/${params.user}/defaultValue`);
    }
  },
  render () { return null; }
});

:+1: for adding to docs

It seems really backwards to me that in order to set a default value for a param, we need to create a new throw-away React component, load that one, transition to a new route, and then load the actual component we want.

It seems as if it would be much more straightforward to be able to have some simple syntax to define what we want a param to be if it's not specified in the url. That way we can simply render the desired data right away, without having to render throw-away components. Something like OP's original solution would be ideal.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

misterwilliam picture misterwilliam  路  3Comments

stnwk picture stnwk  路  3Comments

imWildCat picture imWildCat  路  3Comments

andrewpillar picture andrewpillar  路  3Comments

Waquo picture Waquo  路  3Comments