lazy() loading not loading non component js file in react

1.4k Views Asked by At

I am trying to lazy load constant file in react. This constant file is not react component just simple javascript files like below :

// constant.js

export const customFunction = () => {
}

// component.js
const {
customFunction,
} = React.lazy(() => import('./constant.js'));

This I am not able to find in browser under developer tools source option. Also due to this getting customFunction undefined error. This code work if do normal import. customFunction using under useEffect()

import {
customFunction
} from './constant.js';
1

There are 1 best solutions below

8
Hamza Asif On

Do not wrap import statement inside React.lazy. Instead only use import() which returns a Promise and it gets resolved to the contents of the mentioned file

Here is a working example