1. 程式人生 > >React系列之--專案中React 路由搭建demo

React系列之--專案中React 路由搭建demo

                                  React系列之--專案中React 路由搭建demo

關於react-router網上有很多介紹,在這裡必要嘮叨,其中在專案中react-router和react-router-dom功能是一樣的,引用一個就可以了。

1、react-router4.0以上的介紹。

    發現一篇文章寫得不錯,在這裡也必要嘮叨一次了。請閱:

初探 React Router 4.0

2、搭建的環境

     a、使用create-react-app搭建

      b、版本如下:

{
  "name": "react-project-router",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

3、檔案目錄結構


 5、index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from 'react-router-dom';
import './index.css';
import Root from './router/Routes';
import registerServiceWorker from './registerServiceWorker';

const mountNode = document.getElementById('root');
ReactDOM.render(
    <BrowserRouter>
        <Root />
    </BrowserRouter>,
    mountNode
);
registerServiceWorker();

6、App.js

/*
   App 應用總容器
*/
import React, { Component } from 'react';

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

7、Router檔案,路由配置

/*
   Root, Router 配置
*/
import React from 'react';
import { Route, Switch, Redirect } from 'react-router-dom';

import App from './../App';

import Test from './../containers/Test';
import Home from './../containers/Home';
import Message from './../containers/Message';

const Root = () => (
   <div>
      <Switch>
         <Route
            path="/"
            render={props => (
               <App>
                  <Switch>
                     <Route path="/" exact component={Home} />
                     <Route path="/home" component={Home} />
                     <Route path="/test" component={Test} />
                     <Route path="/message/:id" component={Message} />
                      {/*路由不正確時,預設跳回home頁面*/}
                     <Route render={() => <Redirect to="/" />} />
                  </Switch>
               </App>
            )}
         />
      </Switch>
   </div>
);

export default Root;

8、相關元件程式碼

/*
   Home 主頁
*/
import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class Home extends Component {
   render() {
      return (
          <div id="home-container">
              {/*search,state可以自定義,獲取方法:this.props.location.search,this.props.location.state*/}
              <Link to={{ pathname: '/test', search: '?name=homename', state: { mold: 'add' },aa:'dddd' }} className="home-link">
                  點選跳轉到路由引數search,state使用
              </Link>

          </div>
      );
   }
}

export default Home;



/*
   Test 主頁
*/
import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class Test extends Component {
    constructor (props) {
        super(props)
    }

   render() {
       return (
           <div id="test-container">
               <p>search:{this.props.location.search} </p>
               <p>state:{this.props.location.state.mold} </p>
               <div onClick={() =>  this.props.history.goBack()}>返回上一頁</div>
               <div onClick={() => this.props.history.push('/message/12')}>message頁面</div>
           </div>
       );
   }
}
export default Test;



/*
   Message 主頁
*/
import React, { Component } from 'react';

class Message extends Component {
    constructor (props) {
        super(props)
    }

   render() {
       return (
           <h3>Message{this.props.match.params.id}</h3>
       );
   }
}

export default Message;
9、效果如下

       


原始碼:https://github.com/qishuixian/react/tree/master/react-project-router