Skip to main content
Version: 1.2.0

fetchData

Don't get confused with its name, actually, fetchData is a higher-order component which handles multiple tasks for you in a declarative way:

  • Fetching data from an API endpoint
  • Cancellation of previous requests
  • Transformation of data with Web Worker support
  • Validation of props and data
  • Exposing internal states through ref.
  • Handling loading, error, and blank states
  • GraphQL Client Support (Query and Mutations) through graphQLOptions
  • API polling (Future plan)

We're using it internally in all of our widgets. If there is any 3rd party widget that you think should have the same API as K2 widgets then you're at the right place.

Usage

import { fetchData } from "@k2/utils"
import SomeWidget from "some-library"
const ThirdPartyWidget = ({ data }) => {
return <SomeWidget data={data} />
}
const options = {...}
const EnhancedComponent = fetchData(options)(ThirdPartyWidget)
<EnhancedComponent url="/api/data" />

Types

Options

PropertyRequiredDescriptionType
propsValidatoryesValidate props passed to WrappedComponentprops => true OR Error
dataValidatoryesValidate data passed to WrappedComponentprops => true OR Error
blankStateValidatoryesWhether to show blank state or notboolean
defaultComponentStatesnoOverride loading, error and blank statesComponentStates

Props

PropertyRequiredDescriptionType
datayes if url is not givenData to pass to WrappedComponentany
componentStatesnoOverride loading, error and blank statesComponentStates
urlyes if data is not givenURL of api endpoint to fetch datastring
urlOptionsnoAxios configuration to pass to URLAxiosRequestConfig
graphQLOptionsnoGraphQL options (query and variables) to pass to URLGraphQLOptions
mapDatanoTransform api response data(payload: any, prevData: any, mapDataParams: any) => any
mapDataParamsnoExtra params passed to mapData functionany
runWorkernoIf true, data will be transformed using a web workerboolean
refnoRef of internal states Read MoreReact.Ref
onStatusChangenoCalled whenever status of API call changes(obj: { status: string }) => void

There are some limitations of using a web worker which you can read here.