1.0.1 • Published 4 years ago

@react-hook/previous v1.0.1

Weekly downloads
2,053
License
MIT
Repository
github
Last release
4 years ago

A React hook that stores a value from the previous render.

Quick Start

import * as React from 'react'
import usePrevious from '@react-hook/previous'

const useChanged = (onChange) => {
  const [status, setStatus] = React.useState('off')
  const prevStatus = usePrevious(status)

  React.useEffect(() => {
    if (status !== prevStatus) onChange()
  }, [status])

  return [status, setStatus]
}

API

usePrevious()

const usePrevious: <T extends any>(
  value: T,
  initialValue?: T | undefined
) => T | undefined
ArgumentTypeDefaultRequired?Description
valueTYesThe current value
initialValueT | undefinedundefinedNoThe value returned by the hook when the current value has not yet changed

LICENSE

MIT