
import { useRef, useEffect } from "react"

/**
 * Stores the previous value of a stateful variable. Useful for comparing
 * changes inside useEffect. Example:
 *
 * ```js
 * const [foo, setFoo] = useState()
 * const previousFoo = usePrevious(foo)
 *
 * useEffect(() => {
 *   if (previousFoo === "someValue") {
 *      // ...
 *   }
 * }, [foo])
 * ```
 *
 * @param {*} value
 * @returns {*} previousValue
 */
export default function usePrevious(value) {
  const ref = useRef()

  useEffect(() => {
    ref.current = value
  })

  return ref.current
}
