Skip to content

Latest commit

 

History

History
135 lines (100 loc) · 7.02 KB

File metadata and controls

135 lines (100 loc) · 7.02 KB

Router

In React Router, unlike Ember, nested route do not correspond to nested URL. It gives you complete freedom of what your URL look like. Just because you nest a route doesn't mean you need to append a segment to the URL to match.

Routing is just data: location.hash.substr(1)

Alternatives

Examples

Overview

Nested route is nested UI. You should only nest your routes if your UI is nested. - From Ember

  1. You declare your view hierarchy with nested <Route />
  2. React Router will match the deepest route against the URL and activate the entire tree of routes on that branch, nesting all the UI.
  3. You simply use the this.props.children and it will render the active child route.
const routes = (
  <Route path="/" component={App}>
    <IndexRoute component={Dashboard} />
    <Route path="/inbox" component={Inbox} />
    <Route path="calendar" component={Calendar} />
  </Route>
)

ReactDOM.render(
  <Router history={browserHistory}>{routes}</Router>,
  document.getElementById('app')
)

Whenever a hash change, a callback will be called. This callback is being registered and setup at Router.run(). It just pass in a Handler to be inserted into the view's <RouteHandler />.

It is just like the {{outlet}} in Ember. You can have multiple this.props.children or even named components?

Think of Router as describing your interface rather than a URL structure. Routes are completely independent from the URL of API resources. Even if your API URLs are nested, it does not necessarily mean that your React routes should be.

<Router> and <Route> are 2 different things, even though they are technically React components, but they don't actually create DOM themselves.

It may help to think of a <Route> as an "entry point" into your UI. You don't need a route for every component in your component hierarchy, only for those places where your UI differs based on the URL.

If a route uses a relative path, it builds upon the accumulated path of its ancestors. Nested routes may opt-out of this behavior by using an absolute path.

Dynamic Segments

Explicit static paths match more closely than dynamic paths.

Transition

transition.redirect('/login');
transition.retry();
transition.abort();

Link

Normal HTML link will cause a refresh. Use instead.

<Link to="/home" activeClassName="active" onlyActiveOnIndex>Home</Link>

<IndexLink activeStyle={{color: blue}}>Home</IndexLink>

<Link to={{
  pathname: '/jobs',
  query: { page: '12' }
}}>Page 1</Link>
this.props.params
this.props.location.query

// e.g.
this.props.params.address
this.props.location.query.page

Code Splitting and Partial Application Loading

You no need to define your entire route config up front anymore as 1.0 brings first-class support for code-splitting on routes so that adding routes deep in your app doesn't increase the size of the initial bundle.

DFA and NFA?