Maramal
Maramal

Reputation: 3466

Links don't redirect with React DOM Router

I am trying to use the react-dom-router package within my React app but I am not being "redirected" successfully to the component. It only works when I refresh the page or access via the URL.

This is my App.js:

import React, { Component } from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";

import NavMenu from "./components/NavMenu/NavMenu";
import Contact from "./components/Contact/Contact";
import Home from "./components/Home/Home";

class App extends Component {
  render() {
    return (
      <Router>
        <div>
          <NavMenu />

          <Route exact path='/' component={Home} />
          <Route path='/contact' component={Contact} />
        </div>
      </Router>
    );
  }
}

export default App;

This is my NavbarMenu component's code:

import React, { Component } from "react";
import { Navbar, Nav } from "react-bootstrap";
import { BrowserRouter as Router, Link } from "react-router-dom";

class NavMenu extends Component {
  render() {
    return (
      <Router>
        <Navbar bg='light' expand='lg'>
          <Navbar.Brand>Company name</Navbar.Brand>
          <Nav className='mr-auto'>
            <Nav.Link>
              <Link to='/'>Home</Link>
            </Nav.Link>
            <Nav.Link>
              <Link to='/contact'>Contact</Link>
            </Nav.Link>
          </Nav>
        </Navbar>
      </Router>
    );
  }
}

export default NavMenu;

I guess the code for Home & Contact components aren't relevant.

So, when I visit my React app default page http://localhost:3000/ I see the navbar with their links. But when I click in a link, the URL changes but nothing happens until I refresh the page or access from the URL.

I was following this tutorial to get this done. Any ideas?

Upvotes: 3

Views: 2798

Answers (2)

Tarun Gupta
Tarun Gupta

Reputation: 1

After Searching for long time on Stack, found your answer.

Remove React.StrictMode - it prevents it from redirecting. It actually does but you have to refresh your page to see it.

Above sentence meaning, Go to your index.js file, you will find

<React.StrictMode>
  <App/>
<React.StrictMode>

Remove the React.StrictMode tag, save and reload the project. It should work!

Upvotes: 0

Mayank Shukla
Mayank Shukla

Reputation: 104379

Its s because you are using Router twice, first in App.js and then again in NavMenu. We only need to wrap the App container (entry point) with Router.

Remove the <Router> from NavMenu component. Write it like this:

import React, { Component } from "react";
import { Navbar, Nav } from "react-bootstrap";
import { Link } from "react-router-dom";

class NavMenu extends Component {
  render() {
    return (
      <Navbar bg='light' expand='lg'>
        <Navbar.Brand>Company name</Navbar.Brand>
        <Nav className='mr-auto'>
          <Nav.Link>
            <Link to='/'>Home</Link>
          </Nav.Link>
          <Nav.Link>
            <Link to='/contact'>Contact</Link>
          </Nav.Link>
        </Nav>
      </Navbar>
    );
  }
}

export default NavMenu;

Upvotes: 4

Related Questions