简体   繁体   English

React 路由器 v6 路由问题

[英]React router v6 routing issue

I am trying to create a spring boot application with react code inside it.I have my App.js like this.Only the default route is working and other routes are not working.我正在尝试创建一个 spring 启动应用程序,其中包含反应代码。我有这样的 App.js。只有默认路由有效,其他路由无效。

import './App.css';
import React from 'react';

import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-balham.css';
import "react-datepicker/dist/react-datepicker.css";
import 'react-js-dialog-box/dist/index.css';
import {BrowserRouter, BrowserRouter as Router, Route, Routes} from 'react-router-dom';
import UploadData from './UploadData';
import RoutesOfApplication from './RoutesOfApplication';
import '@szhsin/react-menu/dist/index.css';
import '@szhsin/react-menu/dist/transitions/slide.css';
//const rootElement = document.getElementById("root");

class App extends React.Component {
    render(){
    console.log('In App');
        return(
            
    <BrowserRouter>
      <Routes>
      
        <Route path="/" element={<RoutesOfApplication/>} />
        <Route path="/*" element={<RoutesOfApplication/>} />
        <Route path="/UploadData" element={<UploadData/>}/>
        <Route path="/:id" element={<UploadData/>}/>
      </Routes>
    </BrowserRouter>
        );
    }   

}
export default App;


package.json : "react-dom": "^17.0.2",
    "react": "^17.0.2",
    "react-router-dom": "^6.3.0"

Am i missing something.我错过了什么吗?

It's possible that the path = "/*" on the 2nd path line may be causing the issue.可能是第二条路径线上的path = "/*"导致了这个问题。 If React went in order of routing, then that "/*" would be catching all of the routes and not processing /UploadData.如果 React 按路由顺序运行,那么“/*”将捕获所有路由而不处理 /UploadData。

If this doesn't work, I noticed that you imported BrowserRouter twice from "react-router-dom", maybe delete the second time you imported it as an alias "BrowserRouter as Router"?如果这不起作用,我注意到您从“react-router-dom”导入了 BrowserRouter 两次,也许您第二次将其作为别名“BrowserRouter as Router”导入时删除了?

Hope this works!希望这有效!

if you changed the ordering or removed that path, does it work?如果您更改了顺序或删除了该路径,它是否有效?

The problem here is you have a wildcard path "/*" and a dynamic path "/:id" in the same route.这里的问题是您在同一路由中有一个通配符路径"/*"和一个动态路径"/:id"

The "/:id" route is a dynamic route, so react router doesn't care if its an a actual 32 bit unique id, or the word "hello". "/:id"路由是动态路由,因此 React 路由器不关心它是一个实际的 32 位唯一 ID 还是单词“hello”。 Therefore, it is a wildcard route ( "/*" ) as well, but with the added functionality of providing you with a parameter called id when using the useParams hook.因此,它也是一个通配符路由 ( "/*" ),但具有在使用useParams挂钩时为您提供名为id的参数的附加功能。

In your example, since all the routes are at the same level, the wildcard route will always win due to higher specificity (since it matches itself, the root path "/" , /UploadData and "/:id" ).在您的示例中,由于所有路由都处于同一级别,因此通配符路由将由于更高的特异性而始终获胜(因为它匹配自身、根路径"/"/UploadData"/:id" )。 "/UploadData" can only match itself, "/:id" , and /* so it will always have lower specificity. "/UploadData"只能匹配自身、 "/:id"/* ,因此它总是具有较低的特异性。

To remedy this, you can use relative routing by nesting routes inside routes, and use an index route to match whenever the children don't, instead of a wildcard.为了解决这个问题,您可以通过在路由中嵌套路由来使用相对路由,并使用索引路由来匹配子项不匹配的路由,而不是通配符。 Below is an example.下面是一个例子。

    <BrowserRouter>
      <Routes>
        <Route path="/">
           <Route path="UploadData" element={<UploadData/>}/>
           <Route path=":id" element={<UploadData/>}/>
           <Route index element = {<RoutesOfApplication/>} />
        </Route>
      </Routes>
    </BrowserRouter>

This way, on "/" , "/UploadData" , and "/:id" the right route is applied, and on any routes that don't match, the index is applied.这样,在"/""/UploadData""/:id"上应用正确的路由,并且在任何不匹配的路由上应用索引。 Therefore, our routes are covered with the appropriate logic.因此,我们的路线覆盖了适当的逻辑。

You can read more here on how react router version 6 chooses the right route (based on SPECIFICTY) and how to structure them您可以在此处阅读更多有关 React 路由器版本 6 如何选择正确的路由(基于 SPECIFICTY)以及如何构建它们的信息

Let's understand the components that are rendered with different paths.让我们了解使用不同路径呈现的组件。

    <BrowserRouter>
      <Routes>
        <Route path="/" element={<h1/>Root Route</h1>} />
        <Route path="/*" element={<h1>Catch All Route</h1>} />
        <Route path="/UploadData" element={<h1>Upload Data Route</h1>} />
        <Route path="/:id" element={<h1>Id Route</h1>} />
      </Routes>
    </BrowserRouter>

With your current setup (as shown above) this is what happens:使用您当前的设置(如上所示),会发生以下情况:

  • / renders <h1/>Root Route</h1> , which is desired. /呈现所需的<h1/>Root Route</h1>

  • /UploadData renders <h1>Upload Data Route</h1> , which is also desired. /UploadData呈现<h1>Upload Data Route</h1> ,这也是需要的。
    Note : /UploadData also matches with /:id and /* paths but because path /UploadData is the more specific to others React Router resolves to /UploadData .注意/UploadData也与/:id/*路径匹配,但因为路径/UploadData对其他路径更具体,React Router 解析为/UploadData

  • /1234 renders <h1>Id Route</h1> because it's more specific compared to /* . /1234呈现<h1>Id Route</h1>因为它比/*更具体。

  • /foo/bar renders <h1>Catch All Route</h1> . /foo/bar呈现<h1>Catch All Route</h1>

NOTE : Order of routes is not important with React Router V6, it's the specificity of the matching paths.注意:路由顺序对于 React Router V6 并不重要,它是匹配路径的特殊性。

I guess the expectation is that /foo should resolve to the path="/*" route but in reality it resolves to /:id route because React Router doesn't know what your IDs are.我猜想/foo应该解析为path="/*"路由,但实际上它解析为/:id路由,因为 React Router 不知道你的 ID 是什么。

If you want /foo to show the catchall component you can test the validity of the id inside the component for the id route and if the id is not valid you can render the catchall component.如果您希望/foo显示 catchall 组件,您可以测试id路由组件内部id的有效性,如果 id 无效,您可以呈现 catchall 组件。

Refer the code snippet below:参考下面的代码片段:

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Routes, Route, useParams } from "react-router-dom";

const CatchAllComponent = () => <h1>Catch All Route</h1>;
const IdComponent = () => {
  const { id } = useParams();
  const isIdValid = /[0-9]/.test(id);

  if (isIdValid) {
    return <h1>Id Route</h1>;
  } else {
    return <CatchAllComponent />;
  }
};

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<h1>Root Route</h1>} />
        <Route path="/UploadData" element={<h1>Upload Data Route</h1>} />
        <Route path="/:id" element={<IdComponent />} />
        <Route path="*" element={<CatchAllComponent />} />
      </Routes>
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById("root")
);

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

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