Skip to content

bug: react - maximum depth exceeded from PrivateRoute #21717

@jeffcjohnson

Description

@jeffcjohnson

Bug Report

Ionic version:
[ ] 4.x
[x] 5.x

Current behavior:
Redirecting from a PrivateRoute causes "Maximum update depth exceeded".

Expected behavior:
Redirect to, for example, a "/login" page.

I have been trying to use the PrivateRoute pattern described here without success.

Steps to reproduce:

ionic start ionic-react-redirect-bug conference --type=react
cd ionic-react-redirect-bug

Add the following to src/App.tsx:

interface PrivateRouteProps extends RouteProps {
  component: any;
}

const PrivateRoute = ({component: Component, ...rest}: PrivateRouteProps) => {
  const isLoggedIn = false;

  return (
    <Route
      {...rest}
      render={(routeProps) =>
        isLoggedIn ? (
          <Component {...routeProps} />
        ) : (
          <Redirect to={{pathname: "/login", state: {from: routeProps.location}}}/>
        )
      }
    />
  );
}

In src/App.tsx replace:

<Route path="/support" component={Support} />

with:

<PrivateRoute path="/support" component={Support} />

Comment out useEffect in src/components/Map.tsx if it complains about anything (unrelated to this issue).

ionic serve

Click the Support menu item.

Other information:

In the browser console:

Uncaught Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
    React 4
    unlisten react-router.js:69
    listener history.js:162
    notifyListeners history.js:180
    notifyListeners history.js:179
    setState history.js:300
    replace history.js:414
    confirmTransitionTo history.js:152
    replace history.js:397
    onUpdate react-router.js:309
    componentDidUpdate react-router.js:189
    React 6
    unstable_runWithPriority scheduler.development.js:659
    React 5
    unstable_runWithPriority scheduler.development.js:659
    React 6

Ionic info:

Ionic:

   Ionic CLI       : 6.10.1 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework : @ionic/react 5.2.3

Capacitor:

   Capacitor CLI   : 1.3.0
   @capacitor/core : 1.3.0

Utility:

   cordova-res : not installed
   native-run  : 1.0.0

System:

   NodeJS : v14.4.0 (/usr/local/Cellar/node/14.4.0/bin/node)
   npm    : 6.14.6
   OS     : macOS Catalina

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions