1.2.1 • Published 4 years ago

reapex-plugin-dataloader v1.2.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Reapex dataloader plugin

register the plugin

import { App } from 'reapex'
import dataloaderPlugin from 'reapex-plugin-dataloader'

const app = new App()
// 1. register the plugin
export const { useDataLoader, useLazyDataLoader } = app.use(dataloaderPlugin)

Use react hooks

const LoaderWithHook = () => {
  const loaderStatus = useDataLoader({
    name: 'api2',
    apiCall: mockApi,
  })

  if (loaderStatus.loading) {
    return <div>loading...</div>
  }
  if (loaderStatus.error) {
    return <div>Error!!!</div>
  }
  return <div>{loaderStatus.data ? loaderStatus.data : 'No Data!'}</div>
}

Lazy load

const LoaderWithHook = () => {
  const [loaderStatus, load] = useLazyDataLoader({
    name: 'api2',
    apiCall: mockApi,
  })

  return <button onClick={() => load()}>click to load</button>
}

API

DataLoaderProps: The parameter of useDataloader hook function

PropertyDescriptionTypeDefaultRequired
nameThe key of the data stored in redux state, has to be unique if dataKey is not providedstring-Yes
apiCallA function that returns promise(params?: TPramas) => Promise<any>-Yes
intervalFetch data in an interval if given a none 0(ms) numberboolean0No
paramsThe parameters that passed to apiCall functionTParams = anyundefinedNo
dataKeyFunction that compute a dynamic key based on params(name: string, params?: TParams) => string() => 'default'No
ttlHow much time the cache will valid before making a new data fetching, default 0, no cache. The apiCall function will be called every timenumber0No
shouldIntervalA function the returns true/false to determine whether the interval function call should continue or not(data?: TData) => boolean() => trueNo
onSuccessA function will get called when apiCall run successfully(data?: TData) => any-No
onFailureA function will get called when apiCall throw an exception(error?: Error) => any-No
dataPersisterAn object that configures how to persist the dataDataPersister-No
lazyLoadif dataPersister is configured, it will first use the data from persister then call apiCall to refresh the databoolean-No

useDataLoader() hook

useDataLoader: <TData = any, TParams = any>(props: DataLoaderProps) => [LoaderStatus<TData>, LoadActionCreator]

props: DataLoaderProps

The props: DataLoaderProps are defined in the table above.

LoaderStatus

PropertyDescriptionType
dataThe data that reuturned by apiCallTData
loadingtrue when data is loading, otherwise falseboolean
errorAn Error object if apiCall threw exceptionError
lastUpdateTimeThe timestamp of last time when receiving the data from apiCallnumberundefined`
lastErrorTimeThe timestamp of last time when apiCall threw an exceptionnumberundefined`

LoadActionCreator

load: (params?: TParams) => any

A function to call with params which will trigger the apiCall

DataPersister

export interface DataPersister {
  getItem: (key: string, meta?: Meta) => any
  setItem: (key: string, value: any, meta?: Meta) => any
  removeItem: (key: string, meta?: Meta) => any
}

For example, localStorage data persister:

export const LocalStorageDataPersister = (): DataPersister => {
  const getItem = (key: string) => {
    const data = localStorage.getItem(key)
    return data && JSON.parse(data)
  }

  const setItem = (key: string, value: any) => {
    localStorage.setItem(key, JSON.stringify(value))
  }

  const removeItem = (key: string) => {
    localStorage.removeItem(key)
  }

  return {
    getItem,
    setItem,
    removeItem,
  }
}
1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago