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"}} />
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.
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.