Like React.useState()
but use promise value
Inspired by react-use-promise
yarn add use-promise-state
import React from 'react'
import usePromiseState from 'use-promise-state'
function Example (){
const [{state, error, result}, setPromise] = usePromiseState()
return (
<div>
<div>state: {state}</div>
<div>result: {result || ''}</div>
<button onClick={() => setPromsie(new Promise(resolve => setTimeout(() => resolve('hello'), 10000)))}>start</button>
<button onClick={() => setPromise(null)}>cancel</button>
</div>
)
}
MIT