rrr-lazy
Lazy load component with react && react-router.
Installationg
rrr-lazy requires React 16.2.0 or later.
For npm:
npm install rrr-lazy
For yarn:
yarn add rrr-lazy
IntersectionObserver is required by this library. You can use this polyfill for old browsers https://github.com/w3c/IntersectionObserver/tree/master/polyfill
Usage
Use as a common lazy component
Loading data or do something else with lifecycle hooks
API: <Lazy audoReset={true} root rootMargin render loaderComponent loaderProps onError onLoaded onUnload onUnloaded />
Props
autoReset
Type: Boolean
Default: true
Auto reset Lazy component when history changed (history must be set by setHistory
, see below).
root
Type: String|HTMLElement
Default: null
This value will be used as root for IntersectionObserver (See root.
rootMargin
Type: String
Default: null
This value will be used as rootMargin for IntersectionObserver (See rootMargin.
render(status, props)
Type: Function
Required
status
can be unload
, loading
, loaded
.
props
are props that passed from Lazy
. This is designed for @lazy
, and when you use <Lazy>
component, you may not need it.
loaderComponent
Type: string
Default: div
laoderProps
Type: string
Default: {}
loaderComponent
and loaderProps
is used to create a LoaderComponent when status is unload
or loaded
.
The result of render(status, props)
will be passed to LoaderComponent as children
.
onError()
onLoaded()
onLoading()
onUnload()
API: @lazy
Usage:
Or
options
getComponent
With webpack 2 import()
API: setHistory
Set history instance in order to use autoReset
feature.
Example: