Giter Club home page Giter Club logo

react-router-tutorial's Introduction

React Router Tutorial

Install the create-react-app CLI

npm install -g create-react-app

Create the app with create-react-app CLI

# create with create-react-app PROJECT_NAME
create-react-app router-example
# change into the router-example directory
cd router-example

Install React Router dependancy

npm i react-router --save # install dev

Refactor App

We need to refactor how the application serves components

Create a Root.js file in /src

import React, { Component } from 'react'
import { Link } from 'react-router'
import { Home } from '../../Containers'

export default class Root extends Component {
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    )
  }
}

this.props.children is dynamic and will serve the correct component that matches our route config step

Create routes in ReactDOM.render

/* /src/index.js */
<Router history={browserHistory}>
  <Route path="/" component={Root}>
  </Route>
</Router>

add index component

/* /src/index.js */
<Router history={browserHistory}>
  <Route path="/" component={Root}>
    <IndexRoute component={Home} />
  </Route>
</Router>

add params example

/* /src/index.js */
<Router history={browserHistory}>
  <Route path="/" component={Root}>
    <IndexRoute component={Home} />
    <Route path="/post/:id" component={ParamsExample} />
  </Route>
</Router>

Add query example

/* /src/index.js */
<Router history={browserHistory}>
  <Route path="/" component={Root}>
    <IndexRoute component={Home} />
    <Route path="/post/:id" component={ParamsExample} />
    <Route path="/query" component={QueryExample} />
  </Route>
</Router>

add programmatic routing example

/* /src/index.js */
<Router history={browserHistory}>
  <Route path="/" component={Root}>
    <IndexRoute component={Home} />
    <Route path="/post/:id" component={ParamsExample} />
    <Route path="/query" component={QueryExample} />
    <Route path="programmatic" component={ProgrammaticExample} />
  </Route>
</Router>

add 404

/* /src/index.js */
<Router history={browserHistory}>
  <Route path="/" component={Root}>
    <IndexRoute component={Home} />
    <Route path="/post/:id" component={ParamsExample} />
    <Route path="/query" component={QueryExample} />
    <Route path="*" component={FourOFour}/>
  </Route>
</Router>

Add <Link> Component

Programatic Routing

use history.go

react-router-tutorial's People

Contributors

davidwells avatar

Watchers

 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.