Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 1.07 KB

readme.md

File metadata and controls

45 lines (31 loc) · 1.07 KB

@reactutils/use-previous

NPM NPM

A custom hook that uses the useRef hook internally for storing the previous value.

Installation

npm install @reactutils/use-previous

# or

yarn add @reactutils/use-previous

Usage

import usePrevious from '@reactutils/use-previous';

function App() {
  // State value and setter for our example
  const [count, setCount] = useState<number>(0);

  // Get the previous value (was passed into hook on last render)
  const prevCount: number = usePrevious<number>(count);
  
  // Display both current and previous count value
  return (
    <div>
      <h1>
        Now: {count}, before: {prevCount}
      </h1>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}