-
-
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
1 parent
b3d14e2
commit d6fe267
Showing
6 changed files
with
102 additions
and
1 deletion.
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,36 @@ | ||
# `useLatest` | ||
|
||
React state hook that returns the latest state as described in the [React hooks FAQ](https://reactjs.org/docs/hooks-faq.html#why-am-i-seeing-stale-props-or-state-inside-my-function). | ||
|
||
This is mostly useful to get access to the latest value of some props or state inside an asynchronous callback, instead of that value at the time the callback was created from. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useLatest } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
const latestCount = useLatest(count); | ||
|
||
function handleAlertClick() { | ||
setTimeout(() => { | ||
alert(`Latest count value: ${latestCount.current}`); | ||
}, 3000); | ||
} | ||
|
||
return ( | ||
<div> | ||
<p>You clicked {count} times</p> | ||
<button onClick={() => setCount(count + 1)}>Click me</button> | ||
<button onClick={handleAlertClick}>Show alert</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
const latestState = useLatest = <T>(state: T): MutableRefObject<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
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 { useRef, useEffect, MutableRefObject } from 'react'; | ||
|
||
const useLatest = <T>(value: T): MutableRefObject<T> => { | ||
const latest = useRef<T>(value); | ||
|
||
useEffect(() => { | ||
latest.current = value; | ||
}, [value]); | ||
|
||
return latest; | ||
}; | ||
|
||
export default useLatest; |
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,27 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useLatest } from '../src'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [count, setCount] = React.useState(0); | ||
const latestCount = useLatest(count); | ||
|
||
function handleAlertClick() { | ||
setTimeout(() => { | ||
alert(`Latest count value: ${latestCount.current}`); | ||
}, 3000); | ||
} | ||
|
||
return ( | ||
<div> | ||
<p>You clicked {count} times</p> | ||
<button onClick={() => setCount(count + 1)}>Click me</button> | ||
<button onClick={handleAlertClick}>Show alert</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('State|useLatest', module) | ||
.add('Docs', () => <ShowDocs md={require('../docs/useLatest.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,23 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useLatest from '../src/useLatest'; | ||
|
||
const setUp = () => renderHook(({ state }) => useLatest(state), { initialProps: { state: 0 } }); | ||
|
||
it('should return a ref with the latest value on initial render', () => { | ||
const { result } = setUp(); | ||
|
||
expect(result.current).toEqual({ current: 0 }); | ||
}); | ||
|
||
it('should always return a ref with the latest value after each update', () => { | ||
const { result, rerender } = setUp(); | ||
|
||
rerender({ state: 2 }); | ||
expect(result.current).toEqual({ current: 2 }); | ||
|
||
rerender({ state: 4 }); | ||
expect(result.current).toEqual({ current: 4 }); | ||
|
||
rerender({ state: 6 }); | ||
expect(result.current).toEqual({ current: 6 }); | ||
}); |