-3

the following code is not showing any content on the web screen/page

import './App.css';
import Navbar from "./components/Navbar.jsx";
import Tef from './components/Tef';
import About from './components/About';
import Home from './components/Home';

import {useState} from 'react';
import { BrowserRouter as Router ,Routes as Switch , Route ,Link}from "react-router-dom";

function Main() {



    const [mode, setMode] = useState("Dark")
    const [NavbarIDUS , setNavbarID] = useState("Heading-Nav_D")
    const [divIDUS , setDivID] = useState("Nav-bg_D")
    const [PIDUS , setPID] = useState("title_D")
    const [CIDUS , setCID] = useState("contents_D")


    const lightTheme = () =>
        {
            setMode("Dark")
            setCID("contents_D")
            setDivID("Nav-bg_D")
            setNavbarID("heading-Nav_D")
            setPID("title_D")
            document.body.style.background = "#1E1E1E"
            document.body.style.color = "white"
        }
        const darkTheme = () =>
        {
            setMode("Light")
            setCID("contents")
            setDivID("Nav-bg")
            setNavbarID("heading-Nav")
            setPID("title")
            document.body.style.background = "#EEE"
            document.body.style.color = "black"
        }

    //mode handler
    const modeHandler = (event) =>
    {   
        if(mode === "Dark")
        {
            darkTheme();
        }

        else
        {
            lightTheme();
        }

    }

    //renderer
return(<>
<Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
            <li>
              <Link to="/users">Users</Link>
            </li>
          </ul>
        </nav>

        {/* A <Switch> looks through its children <Route>s and
            renders the first one that matches the current URL. */}
        <Switch>
          <Route path="/about">
            <About />
          </Route>
          <Route path="/">
            <Home />
          </Route>
        </Switch>
      </div>
    </Router>
</>    );


}
export default Main;

so WHAT can i do to make it work like it should

i was expecting to see some content on the screen but i give me a blank screen

i followed a website https://v5.reactrouter.com/web/guides/quick-start but it was not working for me so what should i do???

3
  • What version of react-router are you using? A guide written for version 5 won't work with version 6, and based on the fact you're importing Routes and renaming it to Switch i think you're using version 6. Commented Oct 31, 2022 at 13:42
  • i'm using version 6 Commented Oct 31, 2022 at 13:52
  • Why are you using the tutorials for react-router v5 if you're using react-router v6? The tutorials for that are up on reactrouter.com/main/start/tutorial so this is kind of a problem of your own making: don't follow tutorials for different versions. The semver way of versioning, which react-router adheres to, means that different major versions as incompatible with each other. Commented Oct 31, 2022 at 14:19

1 Answer 1

2

You said you are using version 6, but you have <switch> in your code. That is version 5 syntax. Here is a React Router V6 example.

<BrowserRouter>
  <nav>
    <ul>
      <li>
        <Link to="/">Home</Link>
      </li>
      <li>
        <Link to="/About">About</Link>
      </li>
      <li>
        <Link to="/Users">Users</Link>
      </li>
    </ul>
  </nav>
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="/About" element={<About />} />
    <Route path="/Users" element={<Users />} />
  </Routes>
</BrowserRouter>

Edit: Hooks can only be called inside the body of a function component" occurs for multiple reasons If you are getting an invalid hook error then this is not related to the react router if you have followed the V6 examples correctly. There are a few things that can be causing that issue. Invalid Hook Call Warning

Change

import {useState} from 'react';

To import React, {useState} from 'react';

7
  • no , it didn't work Commented Oct 31, 2022 at 14:21
  • What do you mean it didn't work? You have to give more data than that. Just saying "it didn't work" doesn't allow us to help. Are you using version 5 or version 6? You claim to be using version 6, but you have version 5 code and links. Check your package.json and that will tell you what version you are using. If you are using version 6 then you have to follow that logic and syntax.
    – David
    Commented Oct 31, 2022 at 14:23
  • on console log it says Warning: Invalid hook call. Hooks can only be called inside of the body of a function component Commented Oct 31, 2022 at 14:23
  • ^6.4.2 is the react router version Commented Oct 31, 2022 at 14:25
  • Check my edit. Chances are this error is not related to the react router. You can't use hooks outside of a react functional component. Without seeing more of your code it will be hard to tell what exactly is causing it so I included a couple of articles that might help you out.
    – David
    Commented Oct 31, 2022 at 14:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.