promise-react-component
v0.1.0
Published
A utility React component that renders its contents based on lifecycle state of a specified Promise
Downloads
7
Readme
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 **