-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# `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> | ||
Now: {count}, before: {prevCount} | ||
</p> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
const prevState = usePrevious = <T>(state: T): T; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { usePrevious } from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
const prevCount = usePrevious(count); | ||
|
||
return ( | ||
<div> | ||
<p> | ||
Now: {count}, before: {String(prevCount)} | ||
</p> | ||
<button onClick={() => setCount(value => value + 1)}>+</button> | ||
<button onClick={() => setCount(value => value - 1)}>-</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('State|usePrevious', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/usePrevious.md')} />) | ||
.add('Demo', () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { cleanup, renderHook } from 'react-hooks-testing-library'; | ||
import usePrevious from '../usePrevious'; | ||
|
||
afterEach(cleanup); | ||
|
||
describe('usePrevious', () => { | ||
it('should be defined', () => { | ||
expect(usePrevious).toBeDefined(); | ||
}); | ||
|
||
const hook = renderHook(props => usePrevious(props), { initialProps: 0 }); | ||
|
||
it('should return undefined on initial render', () => { | ||
expect(hook.result.current).toBe(undefined); | ||
}); | ||
|
||
it('should return previous state after update', () => { | ||
hook.rerender(1); | ||
hook.rerender(2); | ||
expect(hook.result.current).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
const usePrevious = <T>(state: T): T | undefined => { | ||
const ref = useRef<T>(); | ||
|
||
useEffect(() => { | ||
ref.current = state; | ||
}); | ||
|
||
return ref.current; | ||
}; | ||
|
||
export default usePrevious; |