React Route: 使用嵌套路由加载评论以及 useRouteMatch 钩子函数

使用嵌套路由,Link 的 to 属性使用实际值,Comment 的 Route 定义则使用占位符。

在定义的 Route 内,可以 呈现任意JSX代码,所以下面这样的代码是合法的:

  <Route path={`/quotes/${params.quoteId}`} exact>
    <div className="centered">
      <Link className="btn--flat" to={`/quotes/${params.quoteId}/comments`}>
        Load Comments
      </Link>
    </div>
  </Route>

comments 没有加载时,显示链接 button,加载 comments 后,因为路由不匹配,button 消失, button 的 Route 具有 exact 属性。

使用 useRouteMatch 钩子函数的目的是,使代码更灵活,避免 App.js 里路由修改后,例如如果将 /quotes 改成 /quote,其他页面里因为路由写死了,到处需要进行同步调整。

import { Fragment } from "react";
import { useParams, Route, Link, useRouteMatch } from "react-router-dom";
import Comments from "./../components/comments/Comments";
import HighlightedQuote from "./../components/quotes/HighlightedQuote";

const DUMMY_QUOTES = [
  { id: "q1", author: "Max", text: "Learning React is fun!" },
  { id: "q2", author: "Max", text: "Learning React is great!" },
];

const QuoteDetail = () => {
  const match = useRouteMatch();
  const params = useParams();

  const quote = DUMMY_QUOTES.find((quote) => quote.id === params.quoteId);
  if (!quote) {
    return <p>No quote found!</p>;
  }

  return (
    <Fragment>
      <HighlightedQuote text={quote.text} author={quote.author} />
      <Route path={match.path} exact>
        <div className="centered">
          <Link className="btn--flat" to={`${match.url}/comments`}>
            Load Comments
          </Link>
        </div>
      </Route>
      <Route path={`${match.path}/comments`}>
        <Comments />
      </Route>
    </Fragment>
  );
};

export default QuoteDetail;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
React Router是一个React的第三方库,它提供了一种在React应用程序中实现路由的方式。React Router DOM是React Router的一个扩展,它为React应用程序提供了一个DOM导航组件。 React Router DOM的嵌套路由是一种将多个路由组件嵌套在一起的技术。这使得我们可以在一个页面上使用多个路由,每个路由有自己的路径和组件。 下面是React Router DOM 6的嵌套路由使用详细步骤: 1. 安装React Router DOM 使用npm或yarn安装React Router DOM npm install react-router-dom 2. 创建父路由组件 在你的React组件中引入BrowserRouter和Route组件,BrowserRouter是一个Router组件的具体实现,它使用HTML5 history API来实现单页应用程序的路由。 import { BrowserRouter as Router, Route } from 'react-router-dom'; const App = () => { return ( <Router> <Route path="/" component={Home} /> </Router> ); } 3. 创建子路由组件 在父路由组件中,我们可以使用Route组件来定义子路由使用嵌套路由的一个好处是可以将路由组件分解为更小的组件。在这里,我们将创建一个包含嵌套路由的组件。 import { Route } from 'react-router-dom'; const Profile = () => { return ( <div> <h2>Profile Page</h2> <Route path="/profile/info" component={Info} /> <Route path="/profile/settings" component={Settings} /> </div> ); } 4. 使用嵌套路由 在父路由组件中,我们可以在Route组件中使用嵌套路由。在这个例子中,我们将使用Profile组件作为子路由。 import { BrowserRouter as Router, Route } from 'react-router-dom'; const App = () => { return ( <Router> <Route path="/" component={Home} /> <Route path="/profile" component={Profile} /> </Router> ); } 5. 创建Link组件 在使用嵌套路由的过程中,我们需要创建Link组件来将路由链接到不同的页面。在这里,我们将创建一个Link组件来将路由链接到Profile组件。 import { Link } from 'react-router-dom'; const Home = () => { return ( <div> <h2>Home Page</h2> <Link to="/profile">Go to Profile</Link> </div> ); } 至此,我们已经完成了React Router DOM 6的嵌套路由使用。在使用React Router DOM 6时,我们需要注意一些变化: - Route组件的component属性已经被替换为element属性,用来指定将要被渲染的React元素。 - Route组件的exact属性默认为true。 - 嵌套路由使用Route组件而不是Switch组件来处理路由匹配问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值