Integrating Suspense and Lazy Loading into a React app can make the app a lot more efficent.
It handles the loading state whilst the app is trying to import a component but does not deal with errors.
const Example = React.Lazy(() => import('./Example))
<Suspense fallback={<p>Loading</p>}>
<Example />
</Suspense>
Usually loading states are dealt with using componentDidMount()
or via Redux; when the component renders it notices there isnโt any data and whilst the app waits it renders a loading icon like a spinner and then the component is rendered again, this isnโt very efficent.
const Example = React.lazy(() => import('./Example))
. . .
<Router>
<Suspense fallback={"Loading"}>
<Switch>
<Route path='/' component={Example} />
</Switch>
</Suspense>
</Router>