Ganesh001
Ganesh001

Reputation: 31

React Router V6 with { BrowserRouter, Routes, Route } routing not working, throws blank page

I'm using latest version of [email protected], [email protected], react-router-dom@^6.4.2.

I created this app with BrowserRouter, Routes, and Route for routing between the components, but it's not working, I'm getting blank page.

I am also getting error like "invalid use of hooks / useref". I Don't know where I'm doing wrong.

Email.tsx

import React from "react";

const EmailPage = () => {
  return (
    <div>
      <hr/>
      <h2>Email</h2>
      <hr/>
      <h3>[email protected]</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"
    ]
  }
}

Upvotes: 2

Views: 1856

Answers (2)

Ganesh001
Ganesh001

Reputation: 31

Issue Resolved: Everything is correct in the code.I followed the below steps and it start working completly fine. -> command this in the project file: 'npm audit fix --force' -> then command : 'npm cache clean --force' -> then restart the IDE and do the 'npm start'

Upvotes: 1

Monali
Monali

Reputation: 171

Please remove this code as HTMLElement and check this link https://codesandbox.io/s/affectionate-monad-p1klrr?file=/src/index.js It is working for me.

Upvotes: 1

Related Questions