Home > Blockchain >  React App goes blank after importing React-Router-Dom
React App goes blank after importing React-Router-Dom

Time:05-26

Hi I'm new to React Js. Before importing React Router it was working fine.But now it build successfully but shows a blank page. Here is my code

App.js

//import ReactDOM from "react-dom/client";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import  'bootstrap/dist/css/bootstrap.min.css';
import Pending from './Pages/Home';
import Home from './Pages/Pending';

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={Home}/>
        
          <Route path="/Pending" element={Pending} />  
         
        </Routes>
      
    </BrowserRouter>
  );
}

Home.js

import React, { Component } from "react";
import  'bootstrap/dist/css/bootstrap.min.css';
import NavBar from '../components/NavBar';

function Home(){
    return(
    <div>
<div>
<NavBar/>
</div>
   <h1>HI</h1>
    </div>
    );
   }

export default Home;

CodePudding user response:

You should specify which version of react-router-dom you are using. So I'm just gonna assume, you are using the latest v6.

In your App.js file, you have to make the following changes in order to work:

import { Routes, Route } from "react-router-dom";
import  'bootstrap/dist/css/bootstrap.min.css';
import Pending from './Pages/Home';
import Home from './Pages/Pending';
    
    export default function App() {
          return (
             <Routes>
               <Route index path="/" element={<Home />}/>
                  <Route path="pending" element={<Pending />} />  
             </Routes>
          );
    }

For more info, please visit the official documentation here!

CodePudding user response:

import './App.css';

import {   BrowserRouter as Router ,  Route, Routes } from "react-router-dom";
import Header from './Components/Header';
import Footer from './Components/Footer';

import Home from './Components/Home';
import About from './Components/About';


function App() {
  return (
    <>
    <Router>
      <Header/>
        <Routes>
            <Route path="/" element={ <Home/> } />
            <Route path="/About" element={ <About/> } />
        </Routes>
      <Footer/>
    </Router>
    </>
  );
}

export default App;
  • Related