Lazy loading
- It is recommended to use
React
’slazy
Suspense
features to implement lazy loading components. See the relevant documentation here - Example:
my-nodestack-app
├── src
│ ├── pages
│ │ │── lazy_load
│ │ │ │── index.js
│ │ │ │── layout.js
│ │ │ │── compontents
│ │ │ │ │── about.js
├── ndsk.config.js
└── package.json
import {lazy,useState,Suspense} from 'react';
const About = lazy(() => import('./compontents/about.js'));
export default (props)=>{
const [value,setValue] = useState(false);
const onClick = async ()=>{
setValue(!value)
}
return (
<html lang="en">
<head>
<title>dynamic load</title>
</head>
<body>
<div>
<button onClick={onClick}>load {!value ? 'about' : 'index'}</button>
</div>
{value ? <Suspense> <About /> </Suspense> : props.children}
</body>
</html>
);
}
export default (props)=>{
return (
<div>index</div>
)
}
export default (props)=>{
return (
<div>about</div>
)
}
- Visit http://localhost:3000/lazy_load
- Note: Files in the
components
directory will not generate routes