3

I have a react app setup with CRA.

In my App.js I would load my css files, components and define my routes.

import 'semantic-ui-css/semantic.min.css';
import './css/global.css';
import './index.css';
import 'react-grid-layout/css/styles.css';
import 'react-resizable/css/styles.css';
import ResetPassword from './pages/ResetPassword';
import ResetPasswordConfirm from './pages/ResetPasswordConfirm';

.... 


return (
          <Router>
                <Switch>
                    <Route path="/reset-password">
                        <ResetPassword intl={intl} />
                    </Route>
                    <Route path="/about-us">
                        ...
                    </Route>
             ....
); 

Now, I want to implement route-based code splitting with React.lazy.

I changed my component imports as follows:

const ResetPassword = lazy(() => import('./pages/ResetPassword'));
const ResetPasswordConfirm = lazy(() => import('./pages/ResetPasswordConfirm'));

and I updated my return method as follows:

return (
      <Router>
                <Suspense fallback={<div>Loading...</div>}>
                    <Switch>
                         <Route path="/reset-password">
                            <ResetPassword intl={intl} />
                        </Route>
                        <Route path="/about-us">
                            ...
                        </Route>

);

The problem I'm facing is that none of my CSS files which I import above in my App.js are being loaded into the components.

I didn't eject my app from CRA so I don't have any custom webpack config.

3
  • I'm experiencing the same issue. Did you find a solution that you could share?? Commented Oct 24, 2022 at 20:02
  • 3
    @kuhnflyfish if I remember correctly, the CSS files were being imported, however they weren't imported in the same order as before and as a result the CSS hierarchy changed and different styles were being applied - I ended up explicitly importing the CSS in the components that needed them instead of doing so in App.js - not the best solution but it resolved my issue. Commented Oct 24, 2022 at 22:30
  • how to fix this but not like above @Alk Commented Nov 2, 2022 at 4:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.