0
votes

i using react-router for my project, in that there's a problem which is for every route "#" sign is added at the start of every router path.. ex": http://localhost:3000/#/login

i want to remove that # sign but i couldn't able solve it my self.

procedure of my routing is in app.js im checking the user is signed in or not if not signed in then he will redirect into /login page.(for that also it is showing path as http://localhost:3000/#/login)

below is the app.js

import React, { Component, Fragment } from "react";
import { HashRouter, Route, Switch, Redirect } from "react-router-dom";
// import { renderRoutes } from 'react-router-config';
import "./App.scss";
import { connect } from "react-redux";
import { loadUser } from "./actions/authActions";

const loading = () => (
  <div className="animated fadeIn pt-3 text-center">Loading....</div>
);

// Containers
const DefaultLayout = React.lazy(() =>
  import("./containers/DefaultLayout/DefaultLayout")
);

// Pages
const Login = React.lazy(() => import("./views/Login/Login"));
const Register = React.lazy(() => import("./views/Register/Register"));
const Page404 = React.lazy(() => import("./views/Page404/Page404"));
const Page500 = React.lazy(() => import("./views/Page500/Page500"));

class App extends Component {
  componentDidMount() {
    this.props.LOADUSER();
  }

  render() {
    return (
      <HashRouter>
        <React.Suspense fallback={loading()}>
          <Switch>
            {!this.props.isAuthenicated ? (
              <Fragment>
                <Redirect from="*" to="/login" />
                <Route
                  exact
                  path="/login"
                  name="Login Page"
                  render={props => <Login {...props} />}
                />
                {/* <Route
                  exact
                  path="/register"
                  name="Register Page"
                  render={(props) => <Register {...props} />}
                />
                <Route
                  exact
                  path="/404"
                  name="Page 404"
                  render={(props) => <Page404 {...props} />}
                />
                <Route
                  exact
                  path="/500"
                  name="Page 500"
                  render={(props) => <Page500 {...props} />}
                /> */}
              </Fragment>
            ) : (
              <Route
                name="Home"
                path="/"
                render={props => <DefaultLayout {...props} />}
              />
            )}
          </Switch>
        </React.Suspense>
      </HashRouter>
    );
  }
}

const mapStateToProps = state => ({
  isAuthenicated: state.auth.isAuthenicated,
  isLoading: state.auth.isLoading,
  error: state.error,
  token: state.auth.token
});

const mapDispachToProps = dispach => {
  return {
    //LOGIN: (newUser) => dispach(login(newUser)),
    LOADUSER: () => dispach(loadUser())
  };
};

export default connect(mapStateToProps, mapDispachToProps)(App);

else he is signed in then im using a component called DefaultLayout Component I will render it. it has all the routes for other usages which is using routes from routes.js.

below is the DefaultLayout Component

import React, { Component, Suspense } from "react";
import { Redirect, Route, Switch } from "react-router-dom";
import * as router from "react-router-dom";
import { Container } from "reactstrap";
import { logout } from "../../actions/authActions";
import { ToastContainer } from "react-toastify";
import Loader from "react-loaders";
import "react-toastify/dist/ReactToastify.css";

import {
  AppHeader,
  AppSidebar,
  AppSidebarFooter,
  AppSidebarForm,
  AppSidebarHeader,
  AppSidebarMinimizer,
  AppBreadcrumb2 as AppBreadcrumb,
  AppSidebarNav2 as AppSidebarNav
} from "@coreui/react";
// sidebar nav config
import _navs from "../../_nav";
// routes config
import routes from "../../routes";
import { connect } from "react-redux";

const DefaultHeader = React.lazy(() => import("./DefaultHeader"));

class DefaultLayout extends Component {
  state = {
    isAuthenicated: true
  };

  loading = () => <Loader type="ball-triangle-path" />;

  signOut(e) {
    e.preventDefault();
    this.props.history.push("/login");
    this.props.LOGOUT();
  }

  render() {
    return (
      <div className="app">
        <AppHeader fixed>
          <Suspense fallback={this.loading()}>
            <DefaultHeader onLogout={e => this.signOut(e)} />
          </Suspense>
        </AppHeader>
        <div className="app-body">
          <AppSidebar fixed display="lg">
            <AppSidebarHeader />
            <AppSidebarForm />
            <Suspense>
              <AppSidebarNav
                navConfig={_navs}
                {...this.props}
                router={router}
              />
            </Suspense>
            <AppSidebarFooter />
            <AppSidebarMinimizer />
          </AppSidebar>
          <main className="main">
            <AppBreadcrumb appRoutes={routes} router={router} />
            <Container fluid>
              <Suspense fallback={this.loading()}>
                <Switch>
                  {routes.map((route, idx) => {
                    return route.component ? (
                      <Route
                        key={idx}
                        path={route.path}
                        exact={route.exact}
                        name={route.name}
                        render={props => (
                          <route.component {...props} {...route.props} />
                        )}
                      />
                    ) : null;
                    //  (
                    //   <Redirect from="*" to="/dashboard" />
                    // );
                  })}

                  <Redirect from="*" to="/" />
                </Switch>
              </Suspense>
              <ToastContainer autoClose={3000} position="bottom-center" />
            </Container>
          </main>
        </div>
      </div>
    );
  }
}

const mapStateToProps = state => ({
  isAuthenicated: state.auth.isAuthenicated,
  error: state.error
});

const mapDispachToProps = dispach => {
  return {
    LOGOUT: () => dispach(logout())
  };
};

export default connect(mapStateToProps, mapDispachToProps)(DefaultLayout);

example of routes.js also below

const routes =[{
 path: "/",
    exact: true,
    name: "Home",

    component: Dashboard
},
{
    path: "/user_overview",
    name: "Users Overview",
    component: Register
  }]

for every route it's showing # can anyone help me to resolve that # sign in the route path?

Thank you!

1

1 Answers

0
votes

You are using HashRouter this is the purpose of this Router.

Usually one uses it in-order to prevent the server to get those routes.

If you want to use real routes just replace it with BrowserRouter.

Pay attention that your server will need to be able to support those routes. navigate to some route say /some/page press reload, make sure that your server return your client code.