GithubHelp home page GithubHelp logo

react-routing-example's Introduction

React Routing Example

Installation

npm install
npm start
open http://localhost:3000

Set up in your own project

First install the react-router package as a dependency:

npm install --save react-router

Then set up the routes in your entrypoint:

// src/index.js

// Import the Routing classes
import { Router, Route, IndexRoute, Link, browserHistory } from 'react-router';

// Import your own components to use in the `render()` function below
import App from './App';
import Welcome from './Welcome';
import About from './About';
import Projects from './Projects';
import Project from './Project';
import PageNotFound from './PageNotFound';

// Set up your routes
ReactDOM.render((
  <Router history={browserHistory}>
    <Route path="/" component={App}>                           // everything will be under the `App` component
      <IndexRoute component={Welcome} />                       // the `Welcome` component will be rendered on `/`
      <Route path="/projects" component={Projects}/>           // the `Projects` component will be rendered on `/projects`
      <Route path="/project/:projectId" component={Project}/>  // the `Project` component will be rendered on `/project/<id>`
      <Route path="/about" component={About}/>                 // the `About` component will be rendered on `/about`
      <Route path="*" component={PageNotFound}/>               // all other routes will render `PageNotFound`
    </Route>
  </Router>
), document.getElementById('root'));

Now, we need to tell the App component to render the component passed on by the Router and we can add links to other paths by using the Link component from React Router.

// src/App.js

import React from 'react';
import { Link } from 'react-router';

class App extends React.Component {
    render() {
        return (
          <div className="container">
            {/* Render some links to navigate */}
            <nav className="main">
              <ul>
                <li><Link to="/">Home</Link></li>
                <li><Link to="/projects">Projects</Link></li>
                <li><Link to="/about">About</Link></li>
              </ul>
            </nav>

            {/* Render the component passed on by the Router */}
            {this.props.children}
          </div>
        );
    }
}

export default App;

Accessing Route params

In our project route, we defined a projectId to be passed on in this way:

      <Route path="/project/:projectId" component={Project}/>  // the `Project` component will be rendered on `/project/<id>`

To access that projectId, we can use the props.params of the component, so in this case we can get the id passed in as:

// src/Project.js

this.props.params.projectId

react-routing-example's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

jayakulhade

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.