最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react-router-dom簡介(推薦)

 更新時間:2022年12月24日 09:34:33   作者:wallowyou  
react-router包含三種類型的組件:路由組件、路由匹配組件?、導(dǎo)航組件,在你使用這些組件的時候,都必須先從react-router-dom引入,這篇文章主要介紹了react-router-dom簡介,需要的朋友可以參考下

react-router-dom

react-router-dom文檔地址: https://reacttraining.com/react-router/

1. 安裝

react-router提供多個包可以單獨使用

packagedescription
react-router路由核心功能
react-router-dom基于react-router提供在瀏覽器運(yùn)行環(huán)境下功能
react-router-nativefor React Native
react-router-configstatic route congif helpers

在瀏覽器中運(yùn)行只需要安裝react-router-dom,reac-router-dom依賴react-router會自動安裝依賴,所以不需要再手動安裝react-router

npm install react-router-dom -S
// 或者
yarn add react-router-dom  

2. 路由組件

react-router包含三種類型的組件:路由組件、路由匹配組件 、導(dǎo)航組件。在你使用這些組件的時候,都必須先從react-router-dom引入。

import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

2.1 路由組件

react-router提供了兩種路由組件: BrowserRouter, HashRouter其中BrowserRouter 是基于HTML5 history API (pushState, replaceState, popstate)事件
當(dāng)然與之對應(yīng)的還有HashRouter是基于window.location.hash。

2.2 路由匹配組件

路由匹配組件有兩種:RouteSwitch,Switch把多個路由組合在一起。
對于一個<Route>組件,可以設(shè)置三種屬性:component、render、children來渲染出對應(yīng)的內(nèi)容。
當(dāng)組件已存在時,一般使用component屬性當(dāng)需要傳遞參數(shù)變量給組件時,需要使用render屬性

2.3 導(dǎo)航組件

有三種常用的導(dǎo)航組件,分別是:<Link>、<NavLink>、<Redirect>。<NavLink>是一種特殊的Link組件,匹配路徑時,渲染的a標(biāo)簽帶有active。

3. 使用

在需要使用router的地方引入react-router-dom

3.1 基本使用

以下是路由的基本使用例子

import React from 'react';
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";
// import logo from './logo.svg';
import './App.css';
import About from './views/About'
import Home from './views/Home'
import Person from './views/Person'

function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
            <li>
              <Link to="/person">Person</Link>
            </li>
          </ul>
        </nav>

        {/* A <Switch> looks through its children <Route>s and
            renders the first one that matches the current URL. */}
        <Switch>
          <Route path="/about">
            <About />
          </Route>
          <Route path="/person">
            <Person />
          </Route>
          <Route path="/">
            <Home />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

export default App;

當(dāng)然此處路由也可以有其他寫法,比如

  <Switch>
      <Route exact path="/" component={Home}></Route>
      <Route path="/about" component={About}></Route>
      <Route path="/person/:id" component={Person}></Route>
      <Route component={NotFind}></Route>
  </Switch>

其中的exact表示的是精確匹配,比如上面

<Route exact path="/" component={Home}></Route>

如果沒有寫精確匹配的化,那么后面的所有路徑都可以匹配到首頁,解決方式就是增加exact或者將此路由放置最后面。

3.2 Route動態(tài)傳參

在一個路由匹配組件上設(shè)置參數(shù),比如說上面的Person組件

   <Route path="/person/:id" component={Person}></Route>

設(shè)置是以:開始然后緊跟key值,然后我們在Person組件中就可以通過獲取props獲取這個參數(shù)值

import React from 'react';
export default class Person extends React.Component {
  constructor(props) {
    super(props);
    console.log(this.props.match.params.id)
  }
  render() {
    const id = this.props.match.params.id
    return (
      <div>
        <h2>個人中心頁面</h2>
        <p>個人id是:{id}</p>
      </div>
    )
  }
}

以上為傳統(tǒng)class組件的寫法,現(xiàn)在可以使用hooks,可以使用useParams,代碼如下:

import React from 'react';
import { useParams } from 'react-router-dom'
const Person = () => {
    const { id } = useParams();
    return (
      <div>
        <h2>個人中心頁面</h2>
        <p>個人id是:{id}</p>
      </div>
    )
}

3.3 嵌套路由

在About頁面有一個嵌套路由代碼示例:

import React from 'react';
import { Link, Switch, Route } from 'react-router-dom'
import Tshirt from './product/Tshirt';
import Jeans from './product/Jeans'
export default class About extends React.Component {
  constructor(props) {
    super(props)
    console.log(this.props.match)
  }
  render() {
    const match = this.props.match;
    return (
      <>
    <nav>
      <Link to={`${match.url}/tshirt`}>Tshirt</Link>| 
      <Link to={`${match.url}/jeans`}>Jeans</Link>
    </nav>
    <Switch>
      <Route path={`${match.path}/tshirt`} component={Tshirt}></Route>
      <Route path={`${match.path}/jeans`} exact component={Jeans}></Route>
    </Switch>
  </>
    )
  }
}

此處如果換成Function的話可以直接使用另一個鉤子函數(shù)useRouteMatch,獲取當(dāng)前匹配的路徑和路由

import { useRouteMatch } from 'react-router-dom'
const About = () => {
    const { path, url }  = useRouteMatch();
    ...省略
}

3.4 路由重定向

Redirect路由重定向,使路由重定向到某個頁面,比如我們經(jīng)常會做的沒有登錄重定向到登錄頁面

<Route exact path="/">
  {loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}</Route>

3.5 滾動還原

大部分情況下,我們需要的是每次導(dǎo)航到某個新頁面的的時候,滾動條都是在頂部,這種比較好實現(xiàn)
hooks版本

import { useEffect } from "react";import { useLocation } from "react-router-dom";

export default function ScrollToTop() {
  const { pathname } = useLocation();

  useEffect(() => {
    window.scrollTo(0, 0);
  }, [pathname]);

  return null;
  }

class版本

import React from "react";import { withRouter } from "react-router-dom";

class ScrollToTop extends React.Component {
  componentDidUpdate(prevProps) {
    if (
      this.props.location.pathname !== prevProps.location.pathname
    ) {
      window.scrollTo(0, 0);
    }
  }

  render() {
    return null;
  }}

我們需要把ScrollToTop組件放在Router里面eg:

function App() {
  return (
    <Router>
      <ScrollToTop />
      <App />
    </Router>
  );}

而對于某些情況下比如一些tab我們希望切換回我們?yōu)g覽過的tab頁時我們希望滾動條滾動到我們之前瀏覽的位置,此處自行去實現(xiàn)。

3.6 路由守衛(wèi)

有時候我們在某個表單頁面填好了表單,然后點擊跳轉(zhuǎn)到另外一個頁面。
這時候我們就需要提醒用戶有未提交的表單。當(dāng)然這一步其實也是在實際的業(yè)務(wù)代碼中實現(xiàn)。

import React, { useState } from "react";
import {
  Prompt
} from "react-router-dom";
 const BlockingForm = ()=>{
  let [isBlocking, setIsBlocking] = useState(false);
  return (
    <form
      onSubmit={event => {
        event.preventDefault();
        event.target.reset();
        setIsBlocking(false);
      }}
    >
      <Prompt
        when={isBlocking}
        message={location =>
          `你是否要跳轉(zhuǎn)到 ${location.pathname}`
        }
      />
      <p>
        Blocking?{" "}
        {isBlocking ? "Yes, click a link or the back button" : "Nope"}
      </p>
      <p>
        <input
          size="50"
          placeholder="輸入值測試路由攔截"
          onChange={event => {
            setIsBlocking(event.target.value.length > 0);
          }}
        />
      </p>
      <p>
        <button>提交表單模擬接觸攔截</button>
      </p>
    </form>
  );
}
export default BlockingForm;

3.7代碼分割

有時候為了避免文件過大加快加載速度,我們需要將代碼分割,將某些路由對應(yīng)的組件只有在點擊的時候再加載js,就是組件的懶加載。
我們使用webpack, @babel/plugin-syntax-dynamic-import,loadable-components實現(xiàn)代碼分割。

首先在.babelrc文件中增加配置

{
  "presets": ["@babel/preset-react"],
  "plugins": ["@babel/plugin-syntax-dynamic-import"]
}

在我們需要懶加載的組件使用loadabel

import React from 'react';
import loadable from '@loadable/component';
const BlockForm = loadable(() => import('../pages/BlockForm/index'), {
  fallback: <Loading />
})

其中BlockForm為懶加載得組件
loadable參考文檔地址 跳轉(zhuǎn)

3.8 withRouter

您可以通過withRouter高階組件訪問history屬性和匹配的Route,
withRouter will pass updated match, location, and history props to the wrapped component whenever it renders.
eg:

import React from "react";import PropTypes from "prop-types";import { withRouter } from "react-router";

// A simple component that shows the pathname of the current locationclass ShowTheLocation extends React.Component {
  static propTypes = {
    match: PropTypes.object.isRequired,
    location: PropTypes.object.isRequired,
    history: PropTypes.object.isRequired
  };

  render() {
    const { match, location, history } = this.props;

    return <div>You are now at {location.pathname}</div>;
  }}

// Create a new component that is "connected" (to borrow redux// terminology) to the router.const ShowTheLocationWithRouter = withRouter(ShowTheLocation);

3.9 其他hooks

之前使用了useParamsuseRouteMatch兩個hook,還有另外兩個hook
useHistoryuseLocation
useHistory
可以訪問到history實例,我們可以通過這個實例訪問某個路由
useLocation
返回location對象

到此這篇關(guān)于react-router-dom簡介的文章就介紹到這了,更多相關(guān)react-router-dom內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于react useState更新異步問題

    關(guān)于react useState更新異步問題

    這篇文章主要介紹了關(guān)于react useState更新異步問題,具有很好的參考價值,希望對大家有所幫助。以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

    2022-08-08
  • react中的useContext具體實現(xiàn)

    react中的useContext具體實現(xiàn)

    useContext是React提供的一個鉤子函數(shù),用于在函數(shù)組件中訪問和使用Context,useContext的實現(xiàn)原理涉及React內(nèi)部的機(jī)制,本文給大家介紹react中的useContext具體實現(xiàn),感興趣的朋友一起看看吧
    2023-11-11
  • React-Native做一個文本輸入框組件的實現(xiàn)代碼

    React-Native做一個文本輸入框組件的實現(xiàn)代碼

    這篇文章主要介紹了React-Native做一個文本輸入框組件的實現(xiàn)代碼,非常具有實用價值,需要的朋友可以參考下
    2017-08-08
  • React useImperativeHandle處理組件狀態(tài)和生命周期用法詳解

    React useImperativeHandle處理組件狀態(tài)和生命周期用法詳解

    React Hooks 為我們提供了一種全新的方式來處理組件的狀態(tài)和生命周期,useImperativeHandle是一個相對較少被提及的Hook,但在某些場景下,它是非常有用的,本文將深討useImperativeHandle的用法,并通過實例來加深理解
    2023-09-09
  • React-Native中一些常用組件的用法詳解(二)

    React-Native中一些常用組件的用法詳解(二)

    這篇文章主要跟大家介紹了關(guān)于React-Native中一些常用組件的用法的相關(guān)資料,其中詳細(xì)介紹了關(guān)于ScrollView組件、ListView組件、Navigator組件、TableBarIOS組件以及網(wǎng)絡(luò)請求等相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • React?Native采用Hermes熱更新打包方案詳解

    React?Native采用Hermes熱更新打包方案詳解

    這篇文章主要介紹了React?Native采用Hermes熱更新打包實戰(zhàn),在傳統(tǒng)的熱更新方案中,我們實現(xiàn)熱更新需要借助code-push開源方案,包括熱更新包的發(fā)布兩種方式詳解,感興趣的朋友一起看看吧
    2022-05-05
  • React Native中導(dǎo)航組件react-navigation跨tab路由處理詳解

    React Native中導(dǎo)航組件react-navigation跨tab路由處理詳解

    這篇文章主要給大家介紹了關(guān)于React Native中導(dǎo)航組件react-navigation跨tab路由處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • React?redux?原理及使用詳解

    React?redux?原理及使用詳解

    這篇文章主要為大家介紹了React?redux?原理及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • React router基礎(chǔ)使用方法詳解

    React router基礎(chǔ)使用方法詳解

    這篇文章主要介紹了React router基礎(chǔ)使用方法,React Router是React生態(tài)系統(tǒng)中最受歡迎的第三方庫之一,近一半的React項目中使用了React Router,下面就來看看如何在React項目中使用
    2023-04-04
  • React移動端項目之pdf預(yù)覽問題

    React移動端項目之pdf預(yù)覽問題

    這篇文章主要介紹了React移動端項目之pdf預(yù)覽問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論

额敏县| 丰宁| 攀枝花市| 邻水| 邯郸县| 县级市| 邛崃市| 阿尔山市| 孝义市| 阿拉善右旗| 醴陵市| 郓城县| 北川| 蕉岭县| 秦皇岛市| 东城区| 恭城| 敦化市| 安宁市| 五家渠市| 平遥县| 罗田县| 凉山| 大足县| 榆树市| 登封市| 高平市| 吴桥县| 牡丹江市| 衡水市| 绵竹市| 临海市| 望奎县| 龙海市| 晋州市| 宽城| 罗甸县| 漯河市| 锡林郭勒盟| 贵溪市| 嵩明县|