nanlabs / nancy.js

This repository contains different React components, hooks, apps and libraries that are used in different projects here at NaN Labs.
https://nanlabs.github.io/nancy.js
MIT License
23 stars 10 forks source link

Add usePrevious hook to @nanlabs/react-hooks #92

Closed ulises-jeremias closed 1 year ago

ulises-jeremias commented 1 year ago

Describe the feature

I would like to request the addition of the usePrevious hook to the @nanlabs/react-hooks library. This hook would be a React state hook that returns the previous state.

Use Case

I often find the need to track the previous state in my components, and having a dedicated hook for this functionality would be very useful.

Proposed Solution

Reference:

const prevState = usePrevious = <T>(state: T): T;

Usage example:

import { usePrevious } from '@nanlabs/react-hooks';

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

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

Other Information

No response

Acknowledgements

Version used

0.5.3

Environment details (OS name and version, etc.)

any