Git Product home page Git Product logo

react-router's Introduction

build status npm package #rackt on freenode

A complete routing library for React

React Router keeps your UI in sync with the URL. It has a simple API with powerful features like lazy code loading, dynamic route matching, and location transition handling built right in. Make the URL your first thought, not an after-thought.

Docs & Help

Note: If you are still using React Router 0.13.x the docs can be found on the 0.13.x branch.

Browser Support

We support all browsers and environments where React runs.

Installation

npm + webpack/browserify

Install using npm:

$ npm install history react-router@latest

Note that you need to also install the history package since it is a peer dependency of React Router and won't automatically be installed for you in npm 3+.

Then with a module bundler or webpack, use as you would anything else:

// using an ES6 transpiler, like babel
import { Router, Route, Link } from 'react-router'

// not using an ES6 transpiler
var Router = require('react-router').Router
var Route = require('react-router').Route
var Link = require('react-router').Link

You can require only the pieces you need straight from the lib directory:

import Router from 'react-router/lib/Router'

UMD

There's also a UMD build in the umd directory:

// using an es6 transpiler, like babel
import { Router, Route, Link } from 'react-router/umd/ReactRouter'

// using globals
var Router = window.ReactRouter.Router;
var Link = window.ReactRouter.Link;
var Route = window.ReactRouter.Route;

CDN

If you just want to drop a <script> tag in your page and be done with it, you can use the UMD/global build hosted on cdnjs.

What's it look like?

import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Link } from 'react-router'

const App = React.createClass({/*...*/})
const About = React.createClass({/*...*/})
// etc.

const Users = React.createClass({
  render() {
    return (
      <div>
        <h1>Users</h1>
        <div className="master">
          <ul>
            {/* use Link to route around the app */}
            {this.state.users.map(user => (
              <li key={user.id}><Link to={`/user/${user.id}`}>{user.name}</Link></li>
            ))}
          </ul>
        </div>
        <div className="detail">
          {this.props.children}
        </div>
      </div>
    )
  }
})

const User = React.createClass({
  componentDidMount() {
    this.setState({
      // route components are rendered with useful information, like URL params
      user: findUserById(this.props.params.userId)
    })
  },

  render() {
    return (
      <div>
        <h2>{this.state.user.name}</h2>
        {/* etc. */}
      </div>
    )
  }
})

// Declarative route configuration (could also load this config lazily
// instead, all you really need is a single root route, you don't need to
// colocate the entire config).
render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About}/>
      <Route path="users" component={Users}>
        <Route path="/user/:userId" component={User}/>
      </Route>
      <Route path="*" component={NoMatch}/>
    </Route>
  </Router>
), document.body)

See more in the Introduction, Advanced Usage, and Examples.

Thanks

React Router was initially inspired by Ember's fantastic router. Many thanks to the Ember team.

Also, thanks to BrowserStack for providing the infrastructure that allows us to run our build in real browsers.

react-router's People

Contributors

aaronshaf avatar acdlite avatar agundermann avatar anything-sys avatar benjohnson avatar bobeagan avatar brigand avatar chentsulin avatar coryhouse avatar dancannon avatar deowk avatar gaearon avatar gmaclennan avatar jaketrent avatar jmreidy avatar joekrill avatar johanneslumpe avatar johnnyreilly avatar kidkarolis avatar knowbody avatar mjackson avatar nhunzaker avatar ryanflorence avatar schniz avatar seanadkinson avatar sophiebits avatar stanleycyang avatar taion avatar timdorr avatar werme avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.