react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Letting components define their own routes?

Open duro opened this issue 11 years ago • 5 comments

Sorry if this was answered elsewhere, but I have been unable to find any examples.

Is it possible to let a Component define its own tested routes.

For example, lets say my main app router looks like this:

<Route name="app" path="/" handler={ require('./App') }>
  <Route name="inbox" handler={ require('./Inbox') }/>
  <Route name="calendar" handler={ require('./Calendar') }/>
  <DefaultRoute handler={ require('./Dashboard') }/>
</Route>

Would it be possible for the Inbox component to define several nested routes for itself that are defined in the Inbox handler itself?

duro avatar Mar 01 '15 02:03 duro

I dont know if its possible via jsx. But you can do it programmatically (ie using the Router api) since 0.12.2. This is not documented officially yet, but its in the commits notes.

See #887 if thats what you want. I have each of those (api driven) routes defined in other handler/file. You can either use parentRoute property in the createRoute's parameter, or use api MainRoute.appendChildRoutes()

vasya10 avatar Mar 01 '15 04:03 vasya10

+1 Would be SUCH a fantastic feature.

auser avatar Mar 01 '15 21:03 auser

You could store your (inner) JSX routes on Handler.statics:

var Handler = React.createClass({
  statics: {
    routes: [<Route .. />, ..]
  }
}

var routes = ( 
  <Route path="/" handler={Handler} >
    {Handler.routes}
  </Route>
);

agundermann avatar Mar 03 '15 14:03 agundermann

Just want to know your opinion on this:

tundrax/react-router@9744495bbe90f7a2b6a412d8c66657909829cdb4

Use new route type Mount. If Mount has child Routes, those will be merged with the children of the handler. props except props.handler are optional. If provided they override the ones on handler routes.

// app.jsx

var Profile = require('./profile');

var ResetPassword = React.createClass({
// extending profile routes
});

var routes = (
  <Route handler={App} path='/'>
    <Mount handler={Profile}>
      // prepend to profile routes
      <Route handler={ResetPassword} name='resetPassword' path='reset-password' />
    </Mount>
    <DefaultRoute handler={Dashboard} />
  </Route>
)
// or override props
<Route handler={App} path='/'>
  <Mount handler={Profile} name='profileV2' path='/profile-v2'>
    <Route handler={ResetPassword} name='resetPassword' path='reset-password' />
  </Mount>
  <DefaultRoute handler={Dashboard} />
</Route>
// profile.jsx

var Show = React.createClass({
// ...
});

var Edit = React.createClass({
// ...
});

var routes = (
  <Route name='profile' path='/profile'>
    <Route handler={Edit} path='edit' />
    <DefaultRoute handler={Show} />
  </Route>
);

var Profile = React.createClass({
  statics: {
    routes: routes
  }
// ...
});

Tested it on deeply nested components with own routes, overriding, extending - works well.

sher avatar Mar 10 '15 09:03 sher

we're exploring this as an alternative API, really useful for partial app loading.

ryanflorence avatar Mar 25 '15 18:03 ryanflorence