Skip to content

jchang6513/use-prev-state

Repository files navigation

usePrevState

A simple hook that returns the previous state

Usage

import React, { useState } from 'react';
import usePrevState from 'use-prev-state';

const Demo = () => {
  const [count, setCount] = useState(0);
  const prevCount = usePrevState(count);

  return (
    <p>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
      <p>
        Now: {count}, before: {prevCount}
      </p>
    </p>
  );
};

About

A simple hook that returns the previous state

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published