简体   繁体   English

嵌套路由在 React Router v6 中不起作用

[英]Nested Routing Not Working in React Router v6

im trying to use nested routing with dynamic values.我正在尝试使用具有动态值的嵌套路由。 Normal Routes in App.js are working but the nested route in QouteDetail is not showing anything. App.js 中的正常路由正在运行,但 QouteDetail 中的嵌套路由未显示任何内容。 console says that " NO ROUTES MATCHED LOCATION ".控制台显示“没有路由匹配位置”。 Can somebody tell me what's wrong.有人可以告诉我出了什么问题吗?

CODE:代码:

import React from 'react';
import {Route , Routes } from "react-router-dom";
import AllQuotes from './components/AllQuotes';
import NewQuote from './components/NewQuote';
import QuoteDetail from './components/QuoteDetail';

function App() {
  return (
    <div>
      <Routes>
        <Route path="/" element={<AllQuotes/>} />
        <Route path="/quotes" element={<AllQuotes/>} />
        <Route path="/new-quotes" element={<NewQuote/>} />
        <Route exact path="/quotes/:quoteID" element={<QuoteDetail/> } /> 
      </Routes>

    </div>
  );
}

export default App;
import React from 'react';
import { Route, Routes , useParams } from 'react-router-dom';
import Comments from './comments/Comments';

function QuoteDetail(props) {
    const params = useParams();
    return (
        <div>
            <h1>QUOTE DETAILS</h1>
            <h2>{params.quoteID}</h2>

            <Routes>
            <Route exact path={`/quotes/${params.quoteID}/comments`}  element= {<Comments/>} />  
            </Routes> 
        </div>
    );
}

export default QuoteDetail;

First, fix the invariant warning for the nested route you are trying to match.首先,修复您尝试匹配的嵌套路由的不变警告。

You rendered descendant `<Routes>` (or called `useRoutes()`) at "/quotes/1234" (under `<Route path="/quotes/:quoteID">`) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.

Please change the parent `<Route path="/quotes/:quoteID">` to `<Route path="/quotes/:quoteID/*">`. 
    in Routes (created by QuoteDetail)
    in QuoteDetail (created by App)
    in App

When nesting Routes components within other Routes components, the paths are built relative from the parent Routes component.当将Routes组件嵌套在其他Routes组件中时,路径是相对于父Routes组件构建的。

Given base Routes component:给定基本Routes组件:

<Routes>
  <Route path="/" element={<AllQuotes/>} />
  <Route path="/quotes" element={<AllQuotes/>} />
  <Route path="/new-quotes" element={<NewQuote/>} />
  <Route path="/quotes/:quoteID/*" element={<QuoteDetail/> } /> 
</Routes>

Use the path "/comments" to build relatively from the parent "/quotes/:quoteID" path.使用路径"/comments"相对于父路径"/quotes/:quoteID"构建。

function QuoteDetail(props) {
  const params = useParams();
  return (
    <div>
      <h1>QUOTE DETAILS</h1>
      <h2>{params.quoteID}</h2>

      <Routes>
        <Route path="/comments" element={<Comments />} />
      </Routes>
    </div>
  );
}

编辑 nested-routing-not-working-in-react-router-v6

Alternative Solution替代解决方案

Alternatively you can move the nested route up to the main Routes component and render them nested there.或者,您可以将嵌套路由向上移动到主Routes组件并将它们嵌套在那里。

App应用程序

<Routes>
  <Route path="/" element={<AllQuotes />} />
  <Route path="/new-quotes" element={<NewQuote />} />
  <Route path="/quotes">
    <Route index element={<AllQuotes />} />
    <Route path=":quoteID" element={<QuoteDetail />}>
      <Route path="comments" element={<Comments />} />
    </Route>
  </Route>
</Routes>

Render an Outlet in QuoteDetail where you want the nested routes to be rendered out.QuoteDetail中呈现一个Outlet ,您希望在其中呈现嵌套路由。

function QuoteDetail(props) {
  const params = useParams();
  return (
    <div>
      <h1>QUOTE DETAILS</h1>
      <h2>{params.quoteID}</h2>
      <Outlet />
    </div>
  );
}

编辑 nested-routing-not-working-in-react-router-v6 (forked)

The parent route path has no trailing "*".父路由路径没有尾随“*”。 This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.这意味着如果你导航得更深,父路由将不再匹配,因此子路由将永远不会呈现。

You should change:你应该改变:

<Route path="/quotes/:quoteID" element={<QuoteDetail/> } /> 

to:到:

<Route path="/quotes/:quoteID/*" element={<QuoteDetail/> } /> 

I don't know why you need to create route in another functional component, you can create nested routes in App.js with react router 6:我不知道为什么你需要在另一个功能组件中创建路由,你可以在 App.js 中使用 React router 6 创建嵌套路由:

<Routes>
...
  <Route path="/quotes/:quoteID" element={<QuoteDetail/> }>
    <Route path="/quotes/:quoteID/coments" element={<Comments/>} />
  </Route>
</Routes>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM