-
-
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
5 changed files
with
105 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,46 @@ | ||
# `useGetSet` | ||
|
||
React state hook that returns state getter function instead of | ||
raw state itself, this prevents subtle bugs when state is used | ||
in nested functions. | ||
|
||
|
||
## Usage | ||
|
||
Below example uses `useGetSet` to increment a number after 1 second | ||
on each click. | ||
|
||
```jsx | ||
import {useGetSet} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [get, set] = useGetSet(0); | ||
const onClick = () => { | ||
setTimeout(() => { | ||
set(get() + 1) | ||
}, 1_000); | ||
}; | ||
|
||
return ( | ||
<button onClick={onClick}>Clicked: {get()}</button> | ||
); | ||
}; | ||
``` | ||
|
||
If you would do this example in a naive way using regular `useState` | ||
hook, the counter would not increment correctly if you click fast multiple times. | ||
|
||
```jsx | ||
const DemoWrong = () => { | ||
const [cnt, set] = useState(0); | ||
const onClick = () => { | ||
setTimeout(() => { | ||
set(cnt + 1) | ||
}, 1_000); | ||
}; | ||
|
||
return ( | ||
<button onClick={onClick}>Clicked: {cnt}</button> | ||
); | ||
}; | ||
``` |
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,40 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {useGetSet} from '..'; | ||
import {useState} from '../react'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [get, set] = useGetSet(0); | ||
const onClick = () => { | ||
setTimeout(() => { | ||
set(get() + 1) | ||
}, 1_000); | ||
}; | ||
|
||
return ( | ||
<button onClick={onClick}>Clicked: {get()}</button> | ||
); | ||
}; | ||
|
||
const DemoWrong = () => { | ||
const [cnt, set] = useState(0); | ||
const onClick = () => { | ||
setTimeout(() => { | ||
set(cnt + 1) | ||
}, 1_000); | ||
}; | ||
|
||
return ( | ||
<button onClick={onClick}>Clicked: {cnt}</button> | ||
); | ||
}; | ||
|
||
storiesOf('useGetSet', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useGetSet.md')} />) | ||
.add('Demo', () => | ||
<Demo/> | ||
) | ||
.add('DemoWrong', () => | ||
<DemoWrong/> | ||
) |
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,16 @@ | ||
import {useState, useRef} from './react'; | ||
|
||
const useGetSet = <T>(initialValue: T): [() => T, (value: T) => void] => { | ||
const [_, update] = useState(undefined); | ||
let state = useRef(initialValue); | ||
|
||
const get = () => state.current; | ||
const set = (value: T) => { | ||
state.current = value; | ||
update(undefined); | ||
}; | ||
|
||
return [get, set]; | ||
}; | ||
|
||
export default useGetSet; |