react-promise
a react.js hook for general promise written in typescript. Let's consider a trivial example: you have a promise such as this
let prom = { }
and you want to make a component, which renders out in it's body 'a value'. Without react-promise, such component looks like this:
Component // you can't use stateless component because you need a state { super thisstate = {} } { prom } { if !thisstateval return null return <div>thisstateval</div> } // or you could use a combination of useEffect and useState hook, which is basically the implementation of this small library
and with react-promise:
; }
API
The only argument can be a promise or a promise resolving thunk:
usePromise promiseOrFn: Promise<T> | Promise<T>
it might be desirable to let the usePromise call the promise returnig function, because you often don't want to do that inside the render of your functional component.
Full state object interface returned by the hook looks like:
install
With npm:
npm i react-promise