React navbar links do not work but no errors in terminal.console. Please help!

This is my first time creating a Navbar in React.

Although the Navbar shows in the output… The links do not take me to the pages as required.

I am using react-router-dom@5.3.0. and the latest react-bootstrap.

Can someone please have a look?

This is my code:

import { Navbar, Container, Nav } from "react-bootstrap";
import {
BrowserRouter as Router,

} from "react-router-dom";

import icon from "../Icons/Icon.png";
import filtericon from "../Icons/Filtericon.png";
import logo from "../logo/Logo.png";
import button from "../images/login-btn.png";
import SearchBar from "./Search";
import "./navbar.css";

export default class Navbarbefore extends Component {
  render() {
    return (
        <div className="nav-container">
            className="gap-3 px-3"
              <Navbar.Brand href="/">
                <img className="main-logo" src={logo} alt="logo" />
              <Navbar.Toggle aria-controls="navbarScroll" />
              <Navbar.Collapse id="navbarScroll">
                <Nav className="me-auto flex-grow-1">
                  <div className="ml-70">
                    <SearchBar />
                  <Nav.Link as={Link} href="/home">
                    {" "}
                  <Nav.Link as={Link} activeKey="/reviews">
                    {" "}
                    <div className="mr-60 ml-74 review"> Review </div>
                  <Nav.Link as={Link} activeKey="/community">
                    {" "}
                    <div className="mr-74 community">Community</div>
                  <Nav.Link activeKey="#">
                    {" "}
                      className="ml-74 notification"
                  <Nav.Link as={Link} activeKey="/login">
                    <img className="ml-74" src={button} alt="log in button" />

This is my code for the my app js:

import Home from "./Pages/homepage/Home";
import Reviews from "./Pages/Reviews";
import Community from "./Pages/Community";
import Login from "./Pages/login/Login";
import Navbarbefore from "./Navbar/Navbarbefore";
import Footer from "./footer/footer-main";

import './App.css';

function App() {
  return (
    <div className="App">
      <Navbarbefore />
      <Home />
          <Route path="/home">
            <Home />
          <Route path="/reviews">
            <Reviews />
          <Route path="/community">
            <Community />
          <Route path="/login">
            <Login />
      <Footer />
export default App;

hope you can help me to find a solution :slight_smile:

thanks :slight_smile:

Hello @hmaryan86 !

I see two possible problems with your code:

  • The NavBar component holds the router. The router should be higher on the hierarchy, in the App.js, so the router “can see” the hole aplication.
  • The second one is a bit more complicated, I will try to explain myself the best I can :slight_smile::
    • You are using <Nav.Link as={Link} href="/home">. This is, as far as I know, for using Nav.Link functionality but using other component “behind the scenes”. So the props you are passing to Nav.Link, will end up in the Link component.
    • With that in mind, if you pass down an href, wich is a totally fine attribute for anchors, But React Router Link has nothing to do with that. That component is expecting a prop called “to”.

When using props like as or Component, libraries used to do something like following:

const ExampleComponent = ({propA, ...ComponentProps}) => (
<Parent propA={propA}>
  <Component {...componentProps}/>

So, if you see a prop like that, think that you are actually using the component you specified, not the parent.

Please let me know if I dind’t explain myself correctly, or if you still have some doubts!