对路由器V6作出React,{BrowserRouter,routes,Routing}路由不起作用,抛出空白页

swvgeqrz  于 2022-10-15  发布在  React
关注(0)|答案(2)|浏览(108)

我使用的是最新版本的react@18.2.0react-dom@18.0.6react-router-dom@^6.4.2
我用BrowserRouterRoutesRoute创建了这个应用程序,用于组件之间的路由,但它不起作用,我得到的是空白页面。
我还收到了类似于“钩子/useref的无效使用”之类的错误。我不知道我哪里做错了。
Email.tsx

import React from "react";

const EmailPage = () => {
  return (
    <div>
      <hr/>
      <h2>Email</h2>
      <hr/>
      <h3>abc@xyz.com</h3>
    </div>
  );
}

export default EmailPage;

Contact.tsx

import React from "react";

const ContactPage = () => {
  return (
    <div>
      <hr/>
      <h2>Contact</h2>
      <hr/>
      <h3>BBSR, Odisha, India</h3>
    </div>
  );
}

export default ContactPage;

App.tsx

import React from "react";

const App = () =>{   
  return(
    <div>
      <h1>Welcome Home</h1>
      <nav>
        <a href="/email">Email</a>
        <a href="/contact">Contact</a>
      </nav>
    </div>
  );
}

export default App;

Index.tsx

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import App from './App';

import EmailPage from './route-components/Email';
import ContactPage from './route-components/Contact';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);

root.render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App/>}/>
      <Route path="email" element={<EmailPage/>}/>
      <Route path="contact" element={<ContactPage/>}/>
    </Routes>
  </BrowserRouter>
);

Package.json

{
  "name": "my-route-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.11.65",
    "@types/react": "^18.0.21",
    "@types/react-dom": "^18.0.6",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.8.4",
    "web-vitals": "^2.1.4",
    "react-router-dom": "^6.4.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
ecfsfe2w

ecfsfe2w1#

请删除此代码as HTMLElement,并检查此链接https://codesandbox.io/s/affectionate-monad-p1klrr?file=/src/index.js,它为我工作。

biswetbf

biswetbf2#

问题已解决:代码中的所有内容都是正确的。我遵循了以下步骤,它开始完全正常工作。->在项目文件中命令:'npm audit fix --force'->然后命令:'npm cache clean --force'->然后重启IDE并执行'npm start'

相关问题