0

I am trying to build a route paths with my imported components. I am doing protected routes for users who are logged in, redirect users who haven't signed in yet with <IsUserRedirect> component. I don't know how to deal with array path routes <IsUserRedirect user=[user] loggedInPath=[<Browse />] path=[<Signin />]>.

This is my App.js file:

import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import {IsUserRedirect, ProtectedRoute} from './helpers/routes';
import { useAuthListener } from './hooks';
import Home from './pages/home'
import Signin from './pages/signin'
import Browse from './pages/browse';
import Signup from './pages/signup'

function App() {
  const { user } = useAuthListener();

  return (
    <>
      <Router>
        <Routes>
          <Route
            path="/signin/*"
            element={
              <IsUserRedirect
                user={user}
                loggedInPath={<Browse />}
                path={<Signin />}
              >
                <Signin />
              </IsUserRedirect>

            }
          />
          <Route
            path="/signup/*"
            element ={
              <IsUserRedirect
                user={user}
                loggedInPath={<Browse />}
                path={<Signup />}
              >
                <Signup />
              </IsUserRedirect>
            }
          />
          <Route
            path="/browse/*"
            element={
              <ProtectedRoute user={user} path={<Browse />}>
                <Browse />
              </ProtectedRoute>
            }
          />
          <Route
            path="*"
            element={
              <IsUserRedirect
                user={user}
                loggedInPath={<Browse />}
                path={<Home />}
              >
                <Home />
              </IsUserRedirect>
            }
          />
        </Routes>
      </Router>   
    </>
  );
}

export default App;

This is the './helpers/routes' file with :

import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
export function IsUserRedirect({ user, loggedInPath, children, ...rest }) {

  return (

    
      <Routes>

        <Route
          {...rest}
          render={() => {
            if (!user) {
              return children;
            }
            if (user) {
              return (
                <Navigate
                  to={{
                    pathname: loggedInPath,
                  }}
                />
              );
            }

            return null;
          }}
        />

      </Routes>
     
  );
}
 
export function ProtectedRoute({ user, children, ...rest }) {

  return ( 

      <Routes>

        <Route
          {...rest}
          render={({ location }) => {
            if (user) {
              return children;
            }
            
            if (!user) {
              return (
                <Navigate
                  to={{
                    pathname: 'signin',
                    state: { from: location },
                  }}
                />
              );
            }

            //if none of
            return null;
          }}
        />
        
      </Routes>
    

  );
}

This is the error I am getting and I can't fix it:

react-dom.development.js:18687 The above error occurred in the <Routes> component:

    at Routes (http://localhost:3000/static/js/bundle.js:86406:5)
    at IsUserRedirect (http://localhost:3000/static/js/bundle.js:4470:5)
    at RenderedRoute (http://localhost:3000/static/js/bundle.js:85940:5)
    at Routes (http://localhost:3000/static/js/bundle.js:86406:5)
    at Router (http://localhost:3000/static/js/bundle.js:86344:15)
    at BrowserRouter (http://localhost:3000/static/js/bundle.js:84598:5)
    at App (http://localhost:3000/static/js/bundle.js:56:62)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.


utils.ts:435 Uncaught TypeError: _route$path.includes is not a function
    at utils.ts:435:1
    at Array.forEach (<anonymous>)
    at flattenRoutes (utils.ts:433:1)
    at matchRoutes (utils.ts:334:1)
    at useRoutes (hooks.tsx:385:1)
    at Routes (components.tsx:393:1)
    at renderWithHooks (react-dom.development.js:16305:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
iRobot
  • 19
  • 5
  • *Where* are you dealing with any arrays? Please [edit] to include all relevant code you are working with as part of a complete [mcve]. – Drew Reese Feb 22 '23 at 17:29
  • I meant a route with several paths, sorry for the confusion. – iRobot Feb 22 '23 at 17:32
  • Ok, where are you trying to use a route with several paths? – Drew Reese Feb 22 '23 at 17:34
  • With } path={}> and } path={}>. For some reason, I am getting an error with these. – iRobot Feb 22 '23 at 17:37
  • Are you trying to implement route protection with this `IsUserRedirect` component? Does this answer regarding implementing route protection help? https://stackoverflow.com/a/66289280/8690857 Can you [edit] the post to include all the relevant code? What is `IsUserRedirect` doing? `IsUserRedirect` is passed these "path" props that don't appear to be paths, but JSX literals. We can't address issues with code we can't see though. Based on the callstack it looks like `IsUserRedirect` is trying to render some descendent routes and this is where the issue is. – Drew Reese Feb 22 '23 at 17:37
  • I would like to see the code for `IsUserRedirect` – white-wolf97 Feb 22 '23 at 17:46
  • I included the , I apologize for the delay. – iRobot Feb 22 '23 at 17:52
  • The RRDv6 `Route` component hasn't any `render` prop. All the routed content is rendered via the `element` prop like the root routes. The route protection components should render either the `children` prop, if wrapping components, or the `Outlet` component if wrapping nested routes. – Drew Reese Feb 22 '23 at 17:54
  • Great input, Thank you so much Mr @Drew-Reese. – iRobot Feb 22 '23 at 18:04

0 Answers0