0

Hi guys I am a newbie to reactjs, I am trying to learn reactjs router. I have this weird thing happening to my Dashboard routes. My App component have two routes "/login" and "/"

'/login' shows up the login screen and the '/' shows up the main dashboard screen. In the dashboard I have sidenavigation component and Main content component. I am trying to nest a set of Routes inside main component. The routes are working fine when i navigate it through the links in the side navigation component. But when I try to click back or forward button in the browser it doesn't work. I am getting a blank screen without anything loaded.

please need your help guys.. any help will be highly appreciated.

Please find the code below.

//App.js

import React, { Component } from "react";

import { BrowserRouter, Route, Switch } from "react-router-dom";
import MainPage from "./Mainpage";
import LoginPage from "./components/pages/LoginPage";
import "./styleFiles/index.css";

class App extends Component {
  render() {
    return (
      <div className="flexible-content">
        <Route path="/" exact component={MainPage} />
        <Route path="/login" component={LoginPage} />
      </div>
    );
  }
}

export default App;

//MainPage.js

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

import SideNavigation from "./components/sideNavigation";
import Footer from "./components/Footer";
import "./styleFiles/index.css";
import DashboardPage from "./components/pages/DashboardPage";
import ProfilePage from "./components/pages/ProfilePage";
import TablesPage from "./components/pages/TablesPage";

import InventoryPage from "./components/pages/InventoryPage";

class MainPage extends Component {
  render() {
    return (
      <BrowserRouter>
        <div className="flexible-content">
          <SideNavigation />
          <main id="content" className="p-5">
            <Switch>
              <Route path={"/"} exact component={DashboardPage} />
              <Route path={"/Dashboard"} exact component={DashboardPage} />
              <Route path="/profile" component={ProfilePage} />
              <Route path="/tables" component={TablesPage} />
              <Route path="/inventory" component={InventoryPage} />
              <Route component={DashboardPage} />
            </Switch>
          </main>
          <Footer />
        </div>
      </BrowserRouter>
    );
  }
}

export default MainPage;


//sideNavigation.js

import React from "react";
import logo from "../assets/logo_.svg";
import { MDBListGroup, MDBListGroupItem, MDBIcon } from "mdbreact";
import { NavLink } from "react-router-dom";
import "../styleFiles/sideBar.css";

const sideNavigation = () => {
  return (
    <div className="sidebar-fixed position-fixed">
      <a href="#!" className="logo-wrapper waves-effect">
        <img alt="MDB React Logo" className="img-fluid" src={logo} />
      </a>
      <MDBListGroup className="list-group-flush">
        <NavLink exact={true} to="/" activeClassName="activeClass">
          <MDBListGroupItem className="bgc">
            <MDBIcon icon="chart-pie" className="mr-3" />
            Testing Dashboard
          </MDBListGroupItem>
        </NavLink>
        <NavLink to="/profile" activeClassName="activeClass">
          <MDBListGroupItem className="bgc">
            <MDBIcon icon="user" className="mr-3" />
            Projects
          </MDBListGroupItem>
        </NavLink>
        <NavLink to="/tables" activeClassName="activeClass">
          <MDBListGroupItem className="bgc">
            <MDBIcon icon="table" className="mr-3" />
            Tables
          </MDBListGroupItem>
        </NavLink>
        <NavLink to="/inventory" activeClassName="activeClass">
          <MDBListGroupItem className="bgc">
            <MDBIcon icon="fas fa-truck-loading" className="mr-3" />
            Inventory
          </MDBListGroupItem>
        </NavLink>
        <NavLink to="/404" activeClassName="activeClass" />
      </MDBListGroup>
    </div>
  );
};

export default sideNavigation;




thanks in advance for your help guys...

Riyesh
  • 63
  • 10

1 Answers1

0

When I made a small change to my code, I could eliminate that weird behavior. the changes were only made to my App.js file. please find the changes below

import React, { Component } from "react";

import { BrowserRouter, Route, Switch } from "react-router-dom";
import MainPage from "./Mainpage";
import LoginPage from "./components/pages/LoginPage";
import "./styleFiles/index.css";

class App extends Component {
  render() {
    return (
      <div className="flexible-content ">
        <Switch>
          <Route path="/Main" exact component={MainPage} />
          <Route path="/login" component={LoginPage} />
          <Route component={MainPage} />
        </Switch>
      </div>
    );
  }
}

export default App;

I added a switch tag and a Generic Not Found route component to my app.js file.

Riyesh
  • 63
  • 10