Skip to content

heineiuo/use-promise-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

usePromiseState()

Like React.useState() but use promise value

Inspired by react-use-promise

Install

yarn add use-promise-state

Usage

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>
  )
}

License

MIT