Avinash Dalvi
Avinash Dalvi

Reputation: 9301

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

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';

Upvotes: 2

Views: 1723

Answers (1)

Hamza Asif
Hamza Asif

Reputation: 357

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

Upvotes: 2

Related Questions