Introduction
Getting started
Install
$ npm i universal-data-loader --saveUsage Example
step 1: say you have an api call
// an api call is simply a function that returns promise
type ApiCall = (args: any) => Promise<any>;const mockApi = () => {
return new Promise((resolve) => {
setTimeout(() => resolve(Math.random()), 1000)
})
}step 2: use DataLoader component
import { DataLoader } from 'universal-data-loader'
// if you are using redux and redux-saga, import this one:
import { ReduxDataLoader as DataLoader } from 'universal-data-loader'
export const App = () =>
<DataLoader name="randomNumber" apiCall={mockApi}>
{
(loader) => {
if (loader.loading) {
return <div>loading...</div>
}
if (loader.error) {
return <div>Error!!!</div>
}
return <div>{loader.data ? loader.data : 'No Data!'}</div>
}
}
</DataLoader>Step 3: If you are using React >= 16.4.0 with the new Context API
(Step 3): If you are using redux and redux-saga
Last updated