0

I'm trying Routing for the first time by following this tutorial. In the tutorial, the instructor has used the react router v5 but I wanted to follow the tutorial with the v6. I have tried to understand how the Outlet should work through some of the tutorials but failed to implement is successfully to my code. Whenever I try to render my page, I get the following error:

No routes matched location "/"

Can someone help me out with how to use Outlet in my Routes.js file so that I can render my page successfully? Here are code to my files :

File App.js

import React from "react";
import { BrowserRouter as Router, Route, Routes} from "react-router-dom";
import { Home, Browse, Signin, Signup } from "./pages";
import * as ROUTES from './constants/routes';
import { IsUserRedirect, ProtectedRoute } from "./helpers/routes";

export default function App() {
  const user = null;

  return (
    <Router>
      <Routes>
        
        {/* if the user is not signed in, we will direct it to the sign in page */}
        <Route
          exact
          path={ROUTES.SIGN_IN}
          element={
            <IsUserRedirect
            user={user}
            loggedInPath={ROUTES.BROWSE}
            path={ROUTES.SIGN_IN}
            >
            <Signin />
            </IsUserRedirect>
          }
        />
        
        {/* if the user doesn't exist then we will direct it to the sign up page */}
        <Route
          exact
          path={ROUTES.SIGN_UP}
          element={
            <IsUserRedirect
            user={user}
            loggedInPath={ROUTES.BROWSE}
            path={ROUTES.SIGN_UP}
            >
            <Signup />
            </IsUserRedirect>
          }
        />

        {/* if the user is successfully signed in then we will direct them to the browse page */}
        <Route 
          exact 
          path={ROUTES.BROWSE}
          element={
            <ProtectedRoute
              user={user}
              path={ROUTES.BROWSE}
            >
              <Browse />
            </ProtectedRoute>
          }
        />

        {/* here the matched leaf outlet error is occuring */}
        {/* <Route 
          exact 
          path={ROUTES.HOME}
          element={
            <ProtectedRoute
              user={user}
              path={ROUTES.HOME}
            >
              <ROUTES.HOME />
            </ProtectedRoute>
          }
        /> */}
        
      </Routes>
    </Router>
  );
}

File Routes.js

import React from 'react';
import { Route, Navigate, Routes, Outlet, useNavigate } from 'react-router-dom';

export function IsUserRedirect({ user, loggedInPath, children, ...rest }) {
    
    <Route 
        {...rest}
        render={() => {
            // if the user doesn't exist then we will render them to the children which can be thier sign-in or sign-up page 
            if (!user) {
                return children;
            }
            if (user) {
                return (
                    <Navigate
                        to={{
                            pathname: loggedInPath,
                        }}
                    />
                );
            }
            return <Outlet />
        }}
    />
    // if (!user) {
    //     return children;
    // }
    // if (user) {
    //     return <Navigate to={{ pathname:loggedInPath }} />;
    // }

    return <Outlet />;
}

export function ProtectedRoute({ user, children, ...rest }) {
    return (
            <Route 
                {...rest}
                render={({ location }) => {
                    if (user) {
                        return children;
                    }

                    if (!user) {
                        return (
                            <Navigate 
                                to={{
                                    pathname: 'signin',
                                    state: { from: location },
                                }}
                            />
                        );
                    }
                    return null;
                }}
            />
    )
}

Here is the file where URLs are stored, i.e., src/constants/routes.js

export const HOME = '/';
export const BROWSE = '/browse';
export const SIGN_UP = '/signup';
export const SIGN_IN = '/signin';

I've tried to use the nested routes and then the Outlet in my Routes.js file, but can't figure out where I'm going wrong.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
sakshi_
  • 15
  • 4
  • For starters it looks like you never return anything from the `IsUserRedirect`, and in the `ProtectedRoute` you return a element, which I am not sure what the purpose is, shouldn't that be just a ? – ncpa0cpl Nov 07 '22 at 10:55
  • @ncpa0cpl yeah, Routes element is not doing anything in the `ProtectedRoute` so I've removed it, but still I'm getting the same error. As for the `IsUserRedirect`, I think according to if the user is signed in or not I returning the page (it looks like that to me), but if I'm wrong in that, please guide me to how to write it properly. – sakshi_ Nov 07 '22 at 11:06
  • The thing is that you create a element in that function, but don't do anything with it. So that function is just equivalent to `const IsUserRedirect = () => ` which doesn't do anything with the user or other props like you think it does. – ncpa0cpl Nov 07 '22 at 11:12

0 Answers0