Search code examples
reactjshyperlinkreact-router-domrouter

React Router Dom how to redirect to Other App.js route when you are in any subRoute of any route


I'm new to react & react router dom v5, also my english is bad. Thank you in advance for helping me.

my problem: I have 2 Main Routes in my App.js route

import { Suspense } from 'react';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'

/* Pges */
import AdminContainer from './Pages/Admin/AdminContainer';
import PublicContainer from './Pages/Public/PublicContainer';
import NotFound from './Pages/NotFound'
import AuthContainer from './Pages/Auth/AuthContainer';

/* Protected Route */

/* Helpers */

function App() {
  console.log("APP")
  return (
    <Suspense fallback={(<p>Loading</p>)}>
      <Router>
        <Switch>
          <Route path="/auth" component={AuthContainer} />
          <Route path="/admin" component={AdminContainer} />
          <Route path="/*" component={PublicContainer} />
          <Route path="*" component={NotFound} />
        </Switch>
      </Router>
    </Suspense>
    )
  }
export default App;

the authcontainer have 2 sub routes "/signin" "/signup"

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

// PAGES
import Signin from "../Auth/Signin";
import Signup from "../Auth/Signup";

const AuthContainer = () => {
  console.log("AUTH")
  return (
    <div>
      <Router>
        <Switch>
          <Route exact path="/auth" component={Signin}/>
          <Route exact path="/auth/signin" component={Signin}/>
          <Route exact path="/auth/signup" component={Signup}/>
        </Switch>
      </Router>
    </div>
  );
};

export default withRouter(AuthContainer);

then the my publiccontainer have 3 sub routes "/" "/product" "/mycart"

/* Dependencies */
import { Route, Switch, BrowserRouter as Router } from 'react-router-dom'

/* Components */
import Header from "../../Components/Header"
import Products from "./Products"
import Home from "./Home"
import UserProfile from "../User/AccountProfile"

import MyCart from '../Public/MyCart'

const PublicContainer = () => {
    console.log("PUBLIC")
    return (
        <div>
            <Router>
                <Header />
                <Switch>
                    <Route exact path='/' render={(props) => <Home />} />
                    <Route exact path='/products' render={(props) => <Products />} />
                    <Route exact path='/mycart' render={(props) => <MyCart isAuth={false} />} />
                </Switch>
               </Router>
        </div>
    )
}

export default PublicContainer

the my cart component will only render if isAuth is true, else it will redirect to "/auth/signin"

import React from 'react'
import { Redirect } from 'react-router'

const MyCart = ({isAuth}) => {
    if(!isAuth)
        return (<Redirect  from='*' to='/auth/signin'></Redirect>)
    return (
        <div>
            my cart
        </div>
    )
}

export default MyCart

The problem is, its trying to redirect to "/auth/signin" but it is still in the "/" page enter image description here

When i hit reload it finally redirect to "/auth/signin"enter image description here

How can i fix this issue, I really appreciate your help

UPDATE

this is overview of my planned routes enter image description here

By the way i think when the mycart isAuth is false then it tries to Link to /auth/signin which causes the link in the top url to correctly point to auth signin, but after that it only checks the subroutes of the publiccontainer instead of checking the app.js routes enter image description here

But when i reload it, it start searching the correct route from the app.js routes which return the expected route & page which is the sign in

enter image description here


Solution

  • I read a almost similar question in terms of only rendering the correct path when hitting refresh/reload

    here React Router works only after refreshing the page

    The problem was i'm wrapping the sub routes with a new router, so i tried removing the Router jsx that is wrapping the Switch> & other subroutes in both AuthContainer.js & PublicContainer.js

    this is the updated AuthContainer.js

    import React from "react";
    import {
      BrowserRouter as Router,
      Switch,
      Route,
      withRouter
    } from "react-router-dom";
    
    // PAGES
    import Signin from "../Auth/Signin";
    import Signup from "../Auth/Signup";
    
    const AuthContainer = () => {
      console.log("AUTH")
      return (
        <div>
            <Switch>
              <Route exact path="/auth/signin" component={Signin}/>
              <Route exact path="/auth/signup" component={Signup}/>
              <Route exact path="/auth" component={Signin}/>
            </Switch>
        </div>
      );
    };
    
    export default withRouter(AuthContainer);
    

    And this is the PublicContainer.js

    /* Dependencies */
    import { Route, Switch } from 'react-router-dom'
    
    /* Components */
    import Header from "../../Components/Header"
    import Products from "./Products"
    import Home from "./Home"
    import UserProfile from "../User/AccountProfile"
    
    import MyCart from '../Public/MyCart'
    
    /* Protected */
    
    const PublicContainer = ({toAuth}) => {
        console.log("PUBLIC")
        return (
            <div>
                    <Header />
                    <Switch>
                        <Route exact path='/products' render={(props) => <Products />} />
                        <Route exact path='/profile' render={(props) => <UserProfile />} />
                        <Route exact path='/mycart' render={(props) => <MyCart />} />
                        <Route exact path='/' render={(props) => <Home />} />
                    </Switch>
            </div>
        )
    }
    
    export default PublicContainer
    

    enter image description here