Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 850 Bytes

README.md

File metadata and controls

32 lines (23 loc) · 850 Bytes

usePrevious

This hook takes in any value. When the value changes, it returns the previous value. This can be useful for when you need to check that a value transitions from one particular state to another.

Usage:

import usePrevious from '@joshreep/captain-hooks/usePrevious'

enum Status {
    Error = 'ERROR',
    Idle = 'IDLE',
    Loading = 'LOADING',
    Success = 'SUCCESS',
}

function useInitialLoading() {
    const status = useSomeAsyncProcess()
    const previousStatus = usePrevious<Status>(status)

    const isInitiallyLoading = previousStatus === undefined && status === Status.Loading
    const isReloading = previousStatus !== undefined && status === Status.Loading

    //... Do what you feel like with that information
}

Signature:

export default function useIdleTimer<T>(value: T): T | undefined