Skip to content

Commit e20a0e7

Browse files
committed
add js version
1 parent e1c0887 commit e20a0e7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

text/0000-use-state-ref.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ useEffect(() => {
2424
}, []);
2525
```
2626

27-
Also, in this example `useCallback` doesn't need a dependency to know `count`'s value, so `useCallback` inside function just performs once. that's awesome
27+
Also, in this example, `useCallback` doesn't need a dependency to know `count`'s value, so `useCallback` inside function just performs once. that's awesome
2828

2929
```js
3030
const [count, setCount, getCount] = useStateRef();
@@ -41,6 +41,8 @@ const handleClick = useCallback(() => {
4141
# Detailed design
4242

4343
A basic of implementation for `useStateRef` that we can use in project is:
44+
45+
TS version:
4446
```js
4547
function useStateRef<T>(initialValue: T): [T, (nextState: T) => void, () => T] {
4648
const [state, setState] = useState(initialValue);
@@ -51,6 +53,17 @@ function useStateRef<T>(initialValue: T): [T, (nextState: T) => void, () => T] {
5153
}
5254
```
5355

56+
JS version:
57+
```js
58+
function useStateRef(initialValue) {
59+
const [state, setState] = useState(initialValue);
60+
const stateRef = useRef(state);
61+
stateRef.current = state;
62+
const getState = useCallback(() => stateRef.current, []);
63+
return [state, setState, getState];
64+
}
65+
```
66+
5467
# Drawbacks
5568

5669
This is a new hook, so we don't have any breaking change, also we can implement that by internal React hooks.

0 commit comments

Comments
 (0)