(精华)2020年7月26日 React react-router-dom的基本使用

举报
愚公搬代码 发表于 2021/10/19 01:17:20 2021/10/19
【摘要】 import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './...
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {HashRouter,BrowserRouter} from 'react-router-dom'

ReactDOM.render(
    <BrowserRouter>
        <App/>
    </BrowserRouter>
, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
import React from 'react'
import {NavLink,Link,Route,Redirect,Switch} from 'react-router-dom'
import About from './views/about'
import Home from './views/home'
class App extends React.Component {
  render () {
    return (
      <div>
        <div className="row">
          <div className="col-xs-offset-2 col-xs-8">
            <div className="page-header">
              <h2>React 路由案例</h2>
            </div>
          </div>
        </div>

        <div className="row">
          <div className="col-xs-2 col-xs-offset-2">
            <div className="list-group">
              {/*导航路由链接*/}
              <NavLink to="/about" className="list-group-item">About</NavLink>
              <NavLink to="/home" className="list-group-item">Home</NavLink>
            </div>
          </div>
          <div className="col-xs-6">
            <div className="panel">
              <div className="panel-body">
                <Switch>{/*只匹配一次*/}
                  <Route path="/about" component={About}></Route>
                  <Route path="/home" component={Home}></Route>
                  <Redirect to="/home"></Redirect>
                </Switch>
              </div>
            </div>
          </div>
        </div>
      </div>
    )
  }
}

export default App


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
import React from 'react'
import {NavLink,Link,Route,Redirect,Switch} from 'react-router-dom'
import News from './news'
import Message from './message'
export default function Home() {
  return (
    <div>
      <h2>Home组件内容</h2>
      <div>
        <ul className="nav nav-tabs">
          <li>
            <NavLink to="/home/news">
            News
            </NavLink>
            
          </li>
          <li>
            <NavLink to="/home/message">
            message
            </NavLink>
          </li>
        </ul>
        <Switch>
          <Route path="/home/news" component={News}></Route>
          <Route path="/home/message" component={Message}></Route>
          <Redirect to="/home/news"></Redirect>
        </Switch>
      </div>
    </div>
  )
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
import React from 'react'
export default function About() {
  return (
    <div>
      <h2>About组件内容</h2>
      <div>
        about组件
      </div>
    </div>
  )
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
import React from 'react'
class News extends React.Component {
    state = {
        newsArr:['news001','news002','news003']
    }
  render () {
      const {newsArr} = this.state
    return (
        newsArr &&newsArr.map((item,index)=><li key={index}>{item}</li>)
    )
  }
}

export default News


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
import React from 'react'
import {NavLink,Link,Route,Redirect,Switch} from 'react-router-dom'
import MessageDetail from './messageDetail'
class Message extends React.Component {
    state = {
        messages:[]
    }
    componentDidMount(){
        setTimeout(()=>{
            const messages = [
                {id:1,title:'m1'},
                {id:2,title:'m2'},
                {id:3,title:'m3'},
            ]
            this.setState({
                messages
            })
        },1000)
    }
    render () {
        const {messages} = this.state
        const show = messages.length ? 'none' : 'block'
        return (
            <div>
                <p style={{display:show}}>loading.......</p>
                <ul>
                    {
                        messages && messages.map((item,index)=>{
                            return (
                                <li key={index}>
                                    <Link to={`/home/message/${item.id}`}>{item.title}</Link>
                                </li>
                            )
                        })
                    }
                </ul>
                <Route path="/home/message/:id" component={MessageDetail}></Route>
            </div>
        )
    }
}

export default Message


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
import React from 'react'
const messageDetails = [
    {id:1,title:'详细信息1',content:'我爱你祖国'},
    {id:2,title:'详细信息2',content:'我爱你父母'},
    {id:3,title:'详细信息3',content:'我爱你老婆'},
]
export default function MessageDetail(props) {
    const id = props.match.params.id//匹配路由参数
    const md = messageDetails.find(md=>md.id == id)
    console.log(md);
    
    // 拿到二级路由穿过来的id值 进行匹配
  return (
    <div>
      <ul>
        <li>id:{md.id}</li>
        <li>title:{md.title}</li>
        <li>content:{md.content}</li>
      </ul>
    </div>
  )
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。

原文链接:codeboy.blog.csdn.net/article/details/107601675

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。