mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
31 lines
652 B
Markdown
31 lines
652 B
Markdown
# `usePrevious`
|
|
|
|
React state hook that returns the previous state as described in the [React hooks FAQ](https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state).
|
|
|
|
## Usage
|
|
|
|
```jsx
|
|
import {usePrevious} from 'react-use';
|
|
|
|
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>
|
|
);
|
|
};
|
|
```
|
|
|
|
## Reference
|
|
|
|
```ts
|
|
const prevState = usePrevious = <T>(state: T): T;
|
|
```
|