1.0.0 • Published 1 year ago

@reactutils/use-previous v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

usePrevious

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

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>
  );
}