简体   繁体   English

React router v6:如何忽略起始路径并评估其余部分

[英]React router v6: How to ignore beginning path and evaluate the rest of it

I'm trying to render the component ProductDetail when the browser goes to any url that ends with /product/:productId, such as http://localhost:3000/collection/323/product/526311418当浏览器转到以 /product/:productId 结尾的任何 url 时,我正在尝试呈现组件 ProductDetail,例如 http://localhost:3000/collection/323/product/526311418

What I have until now:到目前为止我所拥有的:

import { Routes, Route } from 'react-router-dom';

const App = () => {
    return (
        <Routes>
            <Route path='/' element={<Navigation />}>
                <Route index element={<Home />} />
                <Route path='collection/:collectionId' element={<Collection />} />
                <Route path=':AnyValue*/product/:productId' element={<ProductDetail />} />
            </Route>
        </Routes>
    );
}

react-router-dom@6 doesn't use RegExp for any path processing. react-router-dom@6不使用 RegExp 进行任何路径处理。 You will need to explicitly define the routes you want to match.您需要明确定义要匹配的路由。

Example:例子:

<Routes>
  <Route path="/" element={<Navigation />}>
    <Route index element={<Home />} />
    <Route path="collection/:collectionId" element={<Collection />}> // *
      // matches "/collection/323/product/526311418"
      <Route path="product/:productId" element={<ProductDetail />} />
    </Route>
    // matches "product/526311418"
    <Route path="product/:productId" element={<ProductDetail />} />
  </Route>
</Routes>

* Note: For this Collection needs to also render an Outlet for nested routes. * 注意:对于这个Collection ,还需要为嵌套路由渲染一个Outlet

编辑 react-router-v6-how-to-ignore-beginning-path-and-evaluate-the-rest-of-it

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

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