3

So I am working on an ReactJs App which was using React@16.13.1 in the Start. But Recently we have to update our App to the latest version of React. By Upgrading I am facing issue with React Lazy and Suspense and that they are throwing Errors.

It was Working Fine on Older Version (16.13.1).

import React, { lazy, Suspense, Fragment } from 'react';
import { Switch, Redirect, Route } from 'react-router-dom';

This is the array which is configuring Routes.

const routesConfig = [

{
  exact: true,
  path: '/app/training',
  component: () => <Redirect to="/app/training/trainings" />
},
{
  exact: true,
  path: '/app',
  component: () => <Redirect to="/app/reports/dashboard" />
},
{
  exact: true,
  path: '/app/participant',
  component: () => <Redirect to="/app/participant/participants" />
},
{
  exact: true,
  path: '/app/activity',
  component: () => <Redirect to="/app/activity/activities" />
},
{
  exact: true,
  path: '/app/selfPacedCourses',
  component: () => <Redirect to="/app/selfPacedCourses/courses" />
},
{
  exact: true,
  path: '/app/smsAndEmail',
  component: () => <Redirect to="/app/smsAndEmail/logs" />
},
{
  exact: true,
  path: '/app/resource',
  component: () => <Redirect to="/app/resource/resourcePersons" />
},
{
  exact: true,
  path: '/app/stakeholderTraining',
  component: () => <Redirect to="/app/stakeholderTraining/4WOrganizations" />
},
{
  exact: true,
  path: '/app/report',
  component: () => <Redirect to="/app/report/trainings" />
},
{
  exact: true,
  path: '/app/tna',
  component: () => <Redirect to="/app/tna/surveys" />
},
{
  exact: true,
  path: '/404',
  component: lazy(() => import('src/views/pages/Error404View'))
}, ... ]


This is how that Route Config Array is being Utilized

const renderRoutes = routes =>
  routes ? (
    <Suspense fallback={<LoadingScreen />}>
      <Switch>
        {routes.map((route, i) => {
          const Guard = route.guard || Fragment;
          const Layout = route.layout || Fragment;
          const Component = route.component;

          return (
            <Route
              key={i}
              path={route.path}
              exact={route.exact}
              render={props => (
                <Guard>
                  <Layout>
                    {route.routes ? (
                      renderRoutes(route.routes)
                    ) : (
                      <Component {...props} />
                    )}
                  </Layout>
                </Guard>
              )}
            />
          );
        })}
      </Switch>
    </Suspense>
  ) : null;

function Routes() {
  return renderRoutes(routesConfig);
}

export default Routes;

NOTE : This code is working on older Version of React. But It Crashes when I upgrade my package to latest version.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Muhammad Bilal
  • 113
  • 2
  • 8
  • To be clear, it was ***only*** the React version that was updated? No other packages? – Drew Reese Jan 19 '22 at 09:04
  • @DrewReese I have updated other packages, but then I have to restored them one by one to get to the issue. and Issue Only resolved, When I restored react version to it's previous one – Muhammad Bilal Jan 19 '22 at 09:09
  • I see, thanks. Can you update your question to include enough code for a [minimal, complete, and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? Think you could try also creating a *running* codesandbox that reproduces the issue that we could inspect and debug live? – Drew Reese Jan 19 '22 at 09:12
  • Ok, I am on it. – Muhammad Bilal Jan 19 '22 at 09:15
  • @DrewReese Thanks for Your Time. I have Resolved the Issue. Previously I was doing ` npm install react@latest` and it was installing 17.0.2 but now When I only ran ` npm install react` it upgraded to 16.40.0. And Everything is working fine and I am getting my desire result. Still there's no answer in that Case – Muhammad Bilal Jan 19 '22 at 10:09

0 Answers0