Skip to content

Commit

Permalink
make useLoading track unmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Apr 28, 2019
1 parent 25807d6 commit e4d4181
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,18 @@ export function useCheckInput(

export function useLoading() {
const [isLoading, setState] = React.useState(false)
const mount = React.useRef(false)
React.useEffect(() => {
mount.current = true
return () => void (mount.current = false)
}, [])
const load = (aPromise: Promise<any>) => {
setState(true)
return aPromise.finally(() => {
setState(false)
if (mount.current) setState(false)
})
}
return [isLoading, load] as [boolean, <T>(aPromise: Promise<T>) => Promise<T>]
return [isLoading, load] as const
}

export function useKeydown(key: string, handler: Function) {
Expand Down

0 comments on commit e4d4181

Please sign in to comment.