# A React Router Version 2.0 Cheatsheet with examples

**URL:** <https://forum.freecodecamp.org/t/a-react-router-version-2-0-cheatsheet-with-examples/19156>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [July 11, 2016, 2:06am UTC](https://forum.freecodecamp.org/t/a-react-router-version-2-0-cheatsheet-with-examples/19156 "2016-07-11T02:06:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [July 11, 2016, 2:06am UTC](https://forum.freecodecamp.org/t/a-react-router-version-2-0-cheatsheet-with-examples/19156/1 "2016-07-11T02:06:08Z")

</div>

Based on [http://ricostacruz.com/cheatsheets/react-router.html](http://ricostacruz.com/cheatsheets/react-router.html) but updated for v2.0.0 of React Router.

## Basic

```js
import { render } from 'react-dom'
import { Router, Route, browserHistory } from 'react-router'

// Import RootView and NoMatch

render((
  <Router history={browserHistory}>
    <Route path="/" component={RootView} />
    <Route path="*" component={PageNotFound} />
  </Router>
), document.getElementById('app'))

```

## Nesting

```js
import React from 'react'
import { render } from 'react-dom'
import { Router, Route, browserHistory } from 'react-router'

// Import About, Inbox and Messages

class Chrome extends React {
  render () {
    return (
      <div>
        <h1>App</h1>
        <a href="/about">About</a>
        <a href="/inbox">Inbox</a>
        <a href="/messages">Messages</a>
        { this.props.children }
      </div>
    )
  }
}

render((
  <Router history={browserHistory}>
    <Route path="/" component={Chrome}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox} />
      <Route path="messages" component={Messages} />
    </Route>
  </Router>
), document.getElementById('app'))

```

## URL params

```js
class Message extends React {
  componentDidMount() {
    // from the path `/inbox/messages/:id`
    const id = this.props.params.id
    ...

```

## Link

```js
import { Link } from 'react-router'

/* Nav Component */
  ...
  render() {
    ...
    const userId = 10;

    return (
      // Adding params is as simple as appending them to the route
      <Link to={`user/${userId}`}>User 10</Link>

      // Classes can be added to the link element if the current route is the one they link to
      <Link to="login"
        activeClassName="-active">Login</Link>
    )
  }

```

## Other config

```js
<Route path="/">
  <IndexRoute component={Home} />
  <Route path="*" handler={NotFound} />

  // arbitrary/url/login -> /arbitrary/url/sessions/new
  <Redirect from="login" to="sessions/new" />
  // arbitrary/url/about/1 -> /arbitrary/url/profile/1
  <Redirect from="about/:id" to="profile/:id" />

  <Route name="about-user" ... />
  ...

```

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 7, 2017, 1:45am UTC](https://forum.freecodecamp.org/t/a-react-router-version-2-0-cheatsheet-with-examples/19156/2 "2017-08-07T01:45:51Z")

</div>

Closed on August 7, 2017, 1:45am UTC


