react-router

2022-10-02  本文已影响0人  magic_pill

react-router@5 的基本使用

Route 渲染内容的三种方式

<Route
  path="/"
  exact
  children={() => {
    return <Compare />;
  }}
  component={Compare}
  render={() => {
    return <Compare />;
  }}
/>
三种方式的不同之处
children
render
component

Route、Switch、Link、Redirect 的使用

<HashRouter>
  <Link to="/">Home</Link>
  <Link to="/detail">Detail</Link>
  <Link to="/user">User</Link>

  <Switch>
    <Route path="/" exact component={Compare} />
    <Route path="/detail" component={Detail} />
    <Route path="/user" component={User} />
    <Route path="/error" component={ErrorPage} />
    <Redirect from="/*" to="/error" />
  </Switch>
</HashRouter>

动态路由

<HashRouter>
  <Link to="/product/detail">产品说明</Link>
  <Link to="/product/price">产品价格</Link>

  <Switch>
    <Route path="/product/:name" component={DynamicRoute} />
  </Switch>
</HashRouter>
// [route props]:history、location、match
function DynamicRoute({ match }) {
  const { params } = match;
  return (
    <div>
      <h4>DynamicRoute: {params.name}</h4>
    </div>
  );
}

嵌套路由

function DynamicRoute({ match }) {
  console.log("DynamicRoute: ", match);
  const { url, params } = match;
  return (
    <div>
      <h4>DynamicRoute: {params.name}</h4>
      <Link to={url + "/count"}>销售量</Link>
      <Route path={url + "/count"} component={Count} />
    </div>
  );
}
function Count() {
  return <h4>Count</h4>;
}
import { Component, useState } from "react";
import {
  Link,
  Route,
  HashRouter as Router,
  Switch,
  Redirect,
} from "react-router-dom";

export default function RouteUse() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <h4 onClick={() => setCount(count + 1)}>RouteUse-{count}</h4>
      <Router>
        <Link to="/">Home</Link>
        <Link to="/detail">Detail</Link>
        <Link to="/user">User</Link>
        <Link to="/product/detail">产品说明</Link>
        <Link to="/product/price">产品价格</Link>

        <Switch>
          <Route
            path="/"
            exact
            children={() => {
              return <Compare />;
            }}
            component={Compare}
            render={() => {
              return <Compare />;
            }}
          />
          <Route path="/detail" component={Detail} />
          <Route path="/user" component={User} />
          <Route path="/product/:name" component={DynamicRoute} />
          <Route path="/error" component={ErrorPage} />
          <Redirect from="/*" to="/error" />
        </Switch>
      </Router>
    </div>
  );
}

// [route props]:history、location、match
function DynamicRoute({ match }) {
  console.log("DynamicRoute: ", match);
  const { url, params } = match;
  return (
    <div>
      <h4>DynamicRoute: {params.name}</h4>
      <Link to={url + "/count"}>销售量</Link>
      <Route path={url + "/count"} component={Count} />
    </div>
  );
}
function Count() {
  return <h4>Count</h4>;
}

class Compare extends Component {
  componentDidMount() {
    console.log("componentDidMount");
  }
  componentWillUnmount() {
    console.log("componentWillUnmount");
  }
  render() {
    return (
      <div>
        <h4>Compare</h4>
      </div>
    );
  }
}

function Home() {
  return (
    <div>
      <h4>Home</h4>
    </div>
  );
}
function Detail() {
  return (
    <div>
      <h4>Detail</h4>
    </div>
  );
}
function User() {
  return (
    <div>
      <h4>User</h4>
    </div>
  );
}
function ErrorPage() {
  return <h4>404 page</h4>;
}
上一篇下一篇

猜你喜欢

热点阅读