2

React router changes the url but not refreshing the page. Also if I manually type url and hit ENTER then the routes works properly but on clicking the navlink it won't work.

App.js

import './App.css';
import NavLinks from './compnents/Navbar/NavLinks'
import Home from './compnents/Home/Home';
import About from './compnents/About/About';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";


function App() {
  return (
    <>
      <Router>
        <Switch>
          <Route exact path="/" render={() => {
            return (
              <>
                <NavLinks />
                <Home />
              </>
            )
          }}>
          </Route>
          <Route exact path="/about" render={() => {
            return (
              <>
              <h1>Hello from</h1>
                <NavLinks />
                <About />
              </>
            )
          }}>
          </Route>
        </Switch>
      </Router>
    </>
  );
}

export default App;

File for navlink in which Link is added on which it sets the url NavLinks.js

import { useState } from "react";
import { Link } from 'react-router-dom';
import Logo from "./Logo";
import Container from "../GlobalComponents/Container";


const NavLinks = (props) => {
  const [hidden, setHidden] = useState(true);

  return (
    <nav>
      <Container>
        <Logo />
        <i
          onClick={() => setHidden(!hidden)}
          className={hidden ? "fas fa-bars fa-lg" : "fas fa-times fa-lg"}
          id="burgerMenu"
        ></i>
        <Link to='/'>Home</Link>
        <Link to='/about'>About</Link>
      </Container>
    </nav>
  );
};


export default NavLinks;

On click in NavLink it changes url but not loading the view. Kindly help me. I've also tried with other answers but nothing works.

3
  • What is the version you're using? Commented May 31, 2022 at 7:03
  • "react": "^18.1.0" Commented May 31, 2022 at 7:04
  • not react but react-router-dom Commented May 31, 2022 at 7:05

2 Answers 2

0

You can use the render prop in <Route> but if you're using the latest version then it is no more supported.

Instead of that, you've two methods

  1. Replace render with component prop.

  2. <Route path="/">
      {Your component}
    </Route>
    
Sign up to request clarification or add additional context in comments.

4 Comments

Still not working!
you tried both of things?
The problem was with tag <Switch>. It's replaced with tag <Routes> in new version of react-router-dom. Now onwards Switch will no longer works.
@Krenil right..
0

<Link to={} /> only seems works when accessed within the router, if outside of router you would be better off just using <a href={} />.

I have been struggling with the same thing myself before.

When is say within the router I mean something like this:

<Router>
  <head>
    <Link to={"/contactUs"}>Contact us</Link>
  </head>
  <Routes>
    <Route path={"/contactUs"} element={<ContactUs/>} />
  </Routes>
</Router>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.