Skip to content

Add "default route" handler #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mjackson opened this issue Aug 4, 2014 · 11 comments · Fixed by #200
Closed

Add "default route" handler #164

mjackson opened this issue Aug 4, 2014 · 11 comments · Fixed by #200

Comments

@mjackson
Copy link
Member

mjackson commented Aug 4, 2014

Proposal: <Route defaultRouteHandler> specifies a route handler that is rendered when a route matches on the path but none of its children do. It is akin to Ember's "index" routes and gives us a declarative way to specify a default handler for a route instead of using

render: function () {
  return this.props.activeRouteHandler() || somethingElse();
}

You use it like this.

In addition, when a route uses defaultRouteHandler, the value of this.props.activeRouteHandler should automatically use it so the user can write

render: function () {
  return this.props.activeRouteHandler();
}

instead of

render: function () {
  return this.props.activeRouteHandler() || this.props.defaultRouteHandler();
}
@ryanflorence ryanflorence changed the title Add <Routes defaultRouteHandler> prop Add <Routes defaultRouteHandler> prop or <DefaultRoute/> Aug 4, 2014
@ryanflorence ryanflorence changed the title Add <Routes defaultRouteHandler> prop or <DefaultRoute/> Add <Routes defaultRouteHandler> Aug 4, 2014
@mjackson
Copy link
Member Author

mjackson commented Aug 5, 2014

A few variations on this same idea:

1 Use a defaultRouteHandler prop:

<Route path="users/:userID" handler={User} defaultRouteHandler={Details}>
  <Route path="users/:userID/activity" handler={Activity}/>
</Route>

2 Move defaultRouteHandler out into its own component:

<Route path="users/:userID" handler={User}>
  <Route path="users/:userID/activity" handler={Activity}/>
  <!-- A DefaultRoute magically gets its parent's path -->
  <DefaultRoute handler={Details}/>
</Route>

3 Use a layoutHandler prop to make it more explicit that User is used for layout:

<Route path="users/:userID" handler={Details} layoutHandler={User}>
  <Route path="users/:userID/activity" handler={Activity}/>
</Route>

4 Use a Layout component that does the same thing:

<Layout path="users/:userID" handler={User}>
  <Route path="users/:userID" handler={Details}>
  <Route path="users/:userID/activity" handler={Activity}/>
</Layout>

AFAICT, these would all work exactly the same. Only difference is readability/documentation.

@mjackson
Copy link
Member Author

mjackson commented Aug 5, 2014

Looking at no. 2 makes me wonder if all <Route>s without a name/path should inherit path from their parent. Makes a little more sense than assigning them path="/"...

Edit: It would also let us reduce no. 4 to this:

<Layout path="users/:userID" handler={User}>
  <Route path="users/:userID/activity" handler={Activity}/>
  <Route handler={Details}>
</Layout>

@mjackson mjackson changed the title Add <Routes defaultRouteHandler> Add "default route" handler Aug 5, 2014
@ryanflorence
Copy link
Member

I like <DefaultRoute/> the most.

@mjackson
Copy link
Member Author

mjackson commented Aug 6, 2014

Here's the implementation for <DefaultRoute> if we default routes with no name/path to using the path of their parent route (which I really want to do):

Router.DefaultRoute = Router.Route;

@ryanflorence
Copy link
Member

haha, alright, lets do the no-name-inherit, and if it gets confusing, we'll bring in that amazing component.

@ryanflorence
Copy link
Member

s/gets confusing/if we find we are explaining how to use it a lot/

@ryanflorence
Copy link
Member

note, this will require a version bump since an omitted path means something different

@mjackson
Copy link
Member Author

mjackson commented Aug 7, 2014

👍

@mjackson
Copy link
Member Author

The only reason I don't like <DefaultRoute> is because it gives people the idea that they can put it out of order in their route config, which goes contrary to the way we normally traverse the routes.

Should we make an exception in this case? Would that be confusing?

Edit: I should mention that I believe traversing the routes in order is a pretty great thing. It helps a lot with readability and debugging. If we start making exceptions to that rule, we need to be very explicit about it.

@ryanflorence
Copy link
Member

You're right, I would expect to be able to place it before or after any sibling, I think the exception is fine here.

<Route>
  <Route/>
  <DefaultRoute/>
  <Route/>
</Route>

<Route>
  <DefaultRoute/>
  <Route/>
  <Route/>
</Route>

<Route>
  <Route/>
  <Route/>
  <DefaultRoute/>
</Route>

@mjackson
Copy link
Member Author

Yeah, I think I know a clean way to do it. Need to play around with it a little.

mjackson added a commit that referenced this issue Aug 14, 2014
Also, changed behavior of routes with no name, path, or children so
they act as default routes. <DefaultRoute> is essentially just sugar.

Fixes #164
Fixes #193
mjackson added a commit that referenced this issue Aug 14, 2014
Also, changed behavior of routes with no name, path, or children so
they act as default routes. <DefaultRoute> is essentially just sugar.

Fixes #164
Fixes #193
@lock lock bot locked as resolved and limited conversation to collaborators Jan 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants