Home > Back-end >  My Wep Page Show Nothing When i Use Route
My Wep Page Show Nothing When i Use Route

Time:01-10

Hi Guys i want to create a web page that change pages with Navbars so i want to use bootstrap and react-route-dom to create this but before i use bootstrap its show nothing, some kind of errors may occurred that i didn't see. i post my code here, i just create one page and used it in App.jsx file but it's show nothing, but when i use home page as in App file's return function, it works pretty much good and its ok. i can't find any error. when i put Home file in another directory it's just say's :

No routes matched location "/"

but when i put these 3 file's beside each other it say's:

Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page.

here is my index.jsx file:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/pages/App';
import reportWebVitals from './reportWebVitals';

const rootElement = document.getElementById("root");
ReactDOM.render(
    <App/>,
    rootElement
)
;
reportWebVitals();

and here is the App.jsx file:

import '../../App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import React from "react";
import {BrowserRouter as Router, Route, Routes} from 'react-router-dom';
import Home from './home';
import HeaderBar from '../layout/HeaderBar';

function App() {
    return (
        <Router >
            <div>
                <HeaderBar/>
                <Routes>
                    <Route exact path='./pages/home' element={</Home>}></Route>
                </Routes>
            </div>
        </Router>
    );
}

export default App;

And my Home file:

import React from 'react';

const Home = () => {
    return (
        <div className="container">
            <h1 className='display-5 text-uppercase py-5 text-center'>
               Welcome to JavaScript World
            </h1>
        </div>
    );
};

export default Home;

and finally this is my package.json:

{
  "name": "forex_trader",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.1.3",
    "react": "^17.0.2",
    "react-bootstrap": "^2.1.0",
    "react-dom": "^17.0.2",
    "react-router-bootstrap": "^0.26.0",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "styled-components": "^5.3.3",
    "web-vitals": "^2.1.3"
  },
  "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"
    ]
  }
}

edit Solved:

in version 6:

"component" needs to replaced with

"element"

and needs to close

<Route exact path="/" element={<Home/>}></Route>

CodePudding user response:

In your App.jsx file

Can you replace ./pages/home with /pages/home

It should work then

CodePudding user response:

I think your problem is in the App.js, you need to import the Switch of react-router-dom then wrap it below the Router.

  •  Tags:  
  • Related