Skip to content

Latest commit

 

History

History
59 lines (46 loc) · 1009 Bytes

readme.md

File metadata and controls

59 lines (46 loc) · 1009 Bytes

Promise React Component

A utility React component that renders its contents based on lifecycle state of a specified Promise

Installation

$ npm install --save promise-react-component

Or:

$ yarn add --save promise-react-component

Usage

Simple example:

import PromiseComponent from 'promise-react-component';
// ...

return (
  <PromiseComponent
    promise={myApi.fetchData()}
    resolved={MyComponent}
    rejected={MyErrorScreen}
    pending={MyLoadingSpinner}
  />
)

Advanced example:

import PromiseComponent from 'promise-react-component';
// ...

return (
  <PromiseComponent
    promise={myApi.fetchData()}
    resolved={props =>
      props.route === '/some-route'
        ? <Some {...props} />
        : <Other {...props} />
      }
    rejected={error =>
      error.status === '404'
        ? <MyNotFoundScreen />
        : <MyErrorScreen>
      }
    pending={MyLoadingSpinner}
  />
)

** Note: All props are optional **