0

I am new to React. It's My First Program with React - Router When I Am Using React-Router I Don't get any data displaying on the window. It only Shows Blank Page.

My code - profile.js

import React from 'react'
 function Profile()
 { return ( <div>
   <h1>profile</h1> 
   </div> ) }

export default Profile

about.js file

import React from 'react'

function About() {
    return (
        <div>
            <h1>about</h1>
        </div>
    )
}

export default About

App.js

import About from './Container/about'
import Profile from './Container/profile'
import {BrowserRouter, Route} from 'react-router-dom'
function App() {
 return ( 
<div className="App"> 
<BrowserRouter> 
<Route element={About} path='/about' /> 
<Route element={Profile } path='/profile' /> 
</BrowserRouter> 
</div> 
); 
}

export default App;

If anybody Know any Solution Please Reply

I Checked some websites for results but I Don't get any solution from them. I watched some Youtube videos. but they code the same as mine and they got results but I didn't.

2
  • 1
    It will show the blank page because you have no route for "/". By going to "/about" and "/profile" you will be able to see your components Commented Mar 24, 2022 at 12:10
  • @AsadAshraf I am checking '/about' and '/profile' and it also have blank pages Commented Mar 24, 2022 at 13:27

2 Answers 2

1

if you use older version import Switch from react-router-dom

import About from "./Container/about";
import Profile from "./Container/profile";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
function App() {
  return (
    <div className="App">
      <Router>
        <Switch>
          <Route component={About} path="/about" />
          <Route component={Profile} path="/profile" />
       </Switch>
     </Router>
 </div>
);
}

export default App;

if you using latest version ..import Routes from react-router-dom App.js import Routes

    import About from './Container/about'
    import Profile from './Container/profile'
    import {BrowserRouter as Router,Routes, Route} from 'react-router-dom'
    function App() {
     return ( 
      <Router> 
        <div className="App"> 
         <Routes>
          <Route exact path='/about' element={<About />}  /> 
          <Route exact path='/profile' element={<Profile /> }  /> 
         </Routes>
        </div> 
      </Router> 
      ); 
      }
    
     export default App;
Sign up to request clarification or add additional context in comments.

4 Comments

how to import "Routes route "?. there is nothing named 'Routes Route'. It shows error
import { Route, Routes } from "react-router-dom";
There are two different Routes and Route... import {BrowserRouter, Routes, Route} from 'react-router-dom'
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can use Switch from react-router-dom which renders the route exclusively.

Your code would be like this.

import About from "./Container/about";
import Profile from "./Container/profile";
import { BrowserRouter, Route, Switch } from "react-router-dom";
function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Switch>
          <Route component={About} path="/about" />
          <Route component={Profile} path="/profile" />
        </Switch>
      </BrowserRouter>
    </div>
  );
}

export default App;

Check this sandbox where react-router-dom is implemented with dynamic params as well as components.

3 Comments

It Shows 'Switch' is not exported from 'react-router-dom'
I think you are using v6. I've used v5 in the testing code. Will share the updated sandbox in some time. You can check the official documentation. reactrouter.com/docs/en/v6/upgrading/v5
Thank You. I Am Using v6. Documentation helped me 👍

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.