question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

the vite's HMR does not work when I use React.lazy() API for lazyload

See original GitHub issue

Describe the bug

when I use React.lazy(),like this:

// router/index.ts
import React from 'react';

const Home = React.lazy(() => import('../views/Home'));
const About = React.lazy(() => import('../views/About'));

const routes = [
  {
    path: '/',
    exact: true,
    component: Home
  },
  {
    path: '/about',
    exact: true,
    component: About
  },
  {
    path: '/login',
    exact: true,
    component: React.lazy(() => import('../views/login/login'))
  },
  {
    path: '/form-page',
    exact: true,
    component: React.lazy(() => import('../views/form-test/form-page'))
  },
  {
    path: '/props-up',
    exact: true,
    component: React.lazy(() => import('../views/form-test/props-up'))
  }
];

export default routes;

supplement!I use react-router-config manage my routes information,like this:

// App.tsx
import { renderRoutes } from 'react-router-config';
import routes from '@/router';

// other code

render(): JSX.Element {
  return (
    <Router>
      <div className={styles.container}>
        <div className={styles['title-wraper']}>
          <Link to="/">
            <span className={styles['link-title']}>Home</span>
          </Link>
          <span className={styles['link-line']}> | </span>
          <Link to="/about">
            <span className={styles['link-title']}>About</span>
          </Link>
        </div>
        <Switch>
          <React.Suspense fallback={<Loading></Loading>}>
            {renderRoutes(routes)}
          </React.Suspense>
        </Switch>
      </div>
    </Router>
  );
}

when I modify some code in Home.tsx or About.tsx etc,the HMR was not effected.I need refresh the broswer can see my modification.

if I dont use React.lazy() API,the HMR is normal!

Reproduction

I see vite HMR can work,but it nonitor a wrong file. image

image

I modify About.tsx,but vite HMR feed back router/index.ts.

System Info

- OS: win 10
- browser:Chrome 91
- react(react-dom): 17.0.2
- react-router-dom: 5.2.0
- react-router-config: 5.1.1
- vite: 2.4.2

vite.config.ts

import path from 'path';
import { defineConfig } from 'vite';
import reactRefresh from '@vitejs/plugin-react-refresh';
import eslintPlugin from 'vite-plugin-eslint';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh(), eslintPlugin()],
  resolve: {
    alias: { '@': path.join(__dirname, 'src') }
  },
  css: {
    modules: {
      generateScopedName: '[name]__[local]--[hash:base64:5]'
    }
  }
});


### Used Package Manager

npm

### Logs

```shell
no error!

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
iheyunfeicommented, Aug 14, 2021

damn. It looks like if you give function component a name, then both c-r-a and vite example works.

     // Anonymous direct exports like export default function() {}
    // are currently ignored.

reference

import React from 'react'
const Foo = () => ( // works
  <div>foo</div>
)
export default Foo
import React from 'react'
export default () => ( // doesn't works
  <div>foo</div>
)

So, It’s definitely not a bug of vite, but an intentional behavior of react facebook/react#21181.

3reactions
zqinmiaocommented, Sep 5, 2022

I’m using @loadable/component and Function components its worked fine for me.

@guibwl After i used @loadable/component, it works fine。Thank

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using React.js lazy loading when I navigate to a nested route ...
This error indicates that the bundle. js is not loaded which means that it cannot load the other chunks.
Read more >
React Lazy Loading: The Best Complete Guide - CopyCat Blog
Learn how to optimize your app with a complete guide to React lazy loading and code splitting with coding examples, tips, videos, and...
Read more >
Features | Vite
Vite provides first-party HMR integrations for Vue Single File Components and React Fast Refresh.
Read more >
Lazy loading React components - LogRocket Blog
React. lazy() takes as its argument a function that must return a promise by calling import() to load the component. The returned promise ......
Read more >
vite hot reload not working react
HMR didn't work for dynamic imported modules (React) #2719, the vite's HMR does not work when I use React.lazy() API for lazyload #4298...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found