UNPKG

state-hooks

Version:

Essential set of React Hooks for convenient state management.

24 lines (23 loc) 668 B
import { useEffect, useRef } from 'react'; /** * Tracks previous state of a value. * * @param value Props, state or any other calculated value. * @returns Value from the previous render of the enclosing component. * * @example * function Component() { * const [count, setCount] = useState(0); * const prevCount = usePrevious(count); * // ... * return `Now: ${count}, before: ${prevCount}`; * } */ export default function usePrevious(value) { // Source: https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state const ref = useRef(); useEffect(() => { ref.current = value; }); return ref.current; }