React Routing Configuration

Source: Internet
Author: User

Contains link jump and JS trigger jump and pass the reference.

This is the directory structure of the project, the main code is in the SRC directory, src below a new containers folder put some of our components, router folder is configured for routing.

Write in order: code under the Detail folder

Import react from ' react '

class Detail extends React.component {
    render ()} {return
        (
            <p>detail, URL parameter:{this.props.params.id}</p>
        )
    }

Export default Detail
Home

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

class Home extends React.component {
    render () { Return
        (
            <div>
                <p>Home</p>
                <link to= "/list" >to list</link>
            </div>
        )
}

} Export default Home
List

Import react from ' react '
import {hashhistory} from ' React-router '

class List extends React.component {
    rend ER () {
        const ARR = [1, 2, 3] return
        (
            <ul>
                {arr.map (item, index) => {return
                    <li key={ind EX} onclick={this.clickhandler.bind (this, item)}>js jump to {item}</li>
                })}
            </ul>
        )
    }
    ClickHandler (value) {
        hashhistory.push ('/detail/' + value)
    }
}

export Default List
404yemian:

Import react from ' react '

class NotFound extends React.component {
    render () {return
        (
            <p>404 Notfound</p>
        )
    }

Export default NotFound

Under containers There is a APP.JSX total entry file:

Import react from ' react '

class App extends React.component {
    render () {return
        (
            <div>{ This.props.children}</div>
        )
    }

Export default App

Under the Router folder:

Import react from ' react '
import {Router, Route, indexroute} from ' React-router '

import App from '. /containers/app '
import home from '. /containers/home '
import List from '. /containers/list '
import Detail from '. /containers/detail '
import notfound from '. /containers/notfound '

class Routemap extends React.component {
    updatehandle () {
        Console.log (' Each Router change triggers ')
    }
    render () {return
        (
             <router history={this.props.history} onupdate={ This.updateHandle.bind (This)}>
                <route path= '/' component={app}>
                    <indexroute }/>
                    <route path= ' list ' component={list}/>
                    <route path= ' Detail/:id ' component={detail}/>
                    <route path= "*" component={notfound}/>
                </Route>
            </Router>
        )
    }

Export Default Routemap
The final outermost index.js:

Import react from ' react '
import {render} from ' React-dom '
import {hashhistory} from ' React-router '

import Routemap
    from
    './src/router/routemap ' render (<routemap history={hashhistory}/> document.getElementById (' App ')
)
Use of the router version is ^2.8.1, if the download is more than 4.0 version, then the writing is almost completely different from the present, he made a great change, configure the time to pay attention to the router version number.

Project Address Https://github.com/wineSu/myReact/tree/master/src/containers


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.