uselazy
react hook for lazy load and code-split react components or whatever you want.
NOTE: useLazy now handles both dynamic and named imports.
I'm very lazy
Installation
npm install uselazy
or
yarn add uselazy
API
// This it whats takes useLazy: useLazy // array of functions that returns a promise from a dynamic import which // could be an object with a default import or named import // NOTE: please you should wrap this value inside of `useMemo` importFns: Array<Promise< | >>, // this is were you decided when to execute the import shouldImport: boolean ;
Usage
default
import
handles // Text.tsximport React from 'react'; const Text = <p> your beer </p>; ; // App.tsximport React useState useMemo from 'react';import useLazy from 'uselazy'; const imports = import'./Text'; const App = const shouldImport setShouldImport = ; const isLoading result = ; const TextComponent = result; return <div> <h1> very lazy </h1> <button =>Buy me a beer</button> isLoading && <span>some spinner</span> TextComponent && <TextComponent /> </div> ;;
named
imports
handles // Bears.tsximport React from 'react'; const Bears = <p> Bears loves beers </p>; // App.tsximport React useState useMemo from 'react';import useLazy from 'uselazy'; const namedImports = import'./Bears'; const App = const shouldImport setShouldImport = ; const isLoading result = ; const BearsComponent = result; return <div> <h1> very lazy </h1> <button =>Buy me a beer</button> isLoading && <span>some spinner</span> BearsComponent && <BearsComponent /> </div> ;;
default
and named
imports
Or you can handle both // Text.tsximport React from 'react'; const Text = <p> your beer </p>; ; // Bears.tsximport React from 'react'; const Bears = <p> Bears loves beers </p>; // App.tsximport React useState from 'react';import useLazy from 'uselazy'; const imports = import'./Text' import'./Bears'; const App = const shouldImport setShouldImport = ; const isLoading result: Components = ; return <div> <h1> very lazy </h1> <button =>Buy me lots of beers</button> isLoading && <span>some spinner</span> Components && Components </div> ;;
Or other stuff rather than React components
// someUtils.tsimport React from 'react'; const someUtils = : string return ` beers on the way ;)`; ; ; // App.tsximport React useState from 'react';import useLazy from 'uselazy'; const utilsImport = import'./someUtils'; const App = const shouldImport setShouldImport = ; const isLoading result = ; const utils = result; return <div> <button =>Buy me lots of beers</button> isLoading && <span>some spinner</span> utils && <button =>Buy me more beers for my friends!</button> </div> ;;
NOTE
The reason why I'm encouraging to wrap the imports array with useMemo
is because of useEffect's array of dependencies,
that triggers a re-render whatever they change, so if you DON'T wrap it, you'll get an infinite rerendering loop because,
each render the imports array is different. [] === [] // false.
so I giving the developer total control of this, he decides whether the array can change.
more details here: A Complete Guide to useEffect