Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Route shows blank page

I have a problem I am new and I am trying to use React Router but it shows me a blank page whenever I use it Here is my code:

    import React, {Component} from 'react';
import {BrowserRouter as Router, Route} from 'react-router-dom';
class TodoApp extends Component{
    render() {
      return (
        <div className="TodoApp">
            <Router>
                <>
                <Route path="/" element={<WelcomeComponent />} />
                </>
            </Router>
        </div>
      )
    } 
}


class WelcomeComponent extends Component{
    render()
    {
        return <div>
            abcd
        </div>
    }
}

If I delete the route line and instead I write somthing else the code works fine so I know I have a problem with the Route line and I am not sure what is it

like image 278
aaa Avatar asked Nov 23 '25 20:11

aaa


1 Answers

Route can only be child of Routes. Try adding Routes as parent like below and you should see WelcomeComponent loading up.

render() {
    return (
      <div className="TodoApp">
        <Router>
          <Routes>
            <Route path="/" element={<WelcomeComponent />} />
          </Routes>
        </Router>
      </div>
    );
  }
like image 126
Aditya Akella Avatar answered Nov 26 '25 13:11

Aditya Akella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!