-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
use-state.mts
33 lines (28 loc) · 1.06 KB
/
use-state.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { withPointer, handleChange } from './hook-engine.mjs';
// eslint-disable-next-line @typescript-eslint/ban-types
type NotFunction<T> = T extends Function ? never : T;
export function useState<Value>(
defaultValue: NotFunction<Value> | (() => Value),
): [Value, (newValue: Value) => void];
export function useState<Value>(
defaultValue?: NotFunction<Value> | (() => Value),
): [Value | undefined, (newValue?: Value | undefined) => void];
export function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)) {
return withPointer<Value, [Value, (newValue: Value) => void]>((pointer) => {
const setFn = (newValue: Value) => {
// Noop if the value is still the same.
if (pointer.get() !== newValue) {
pointer.set(newValue);
// Trigger re-render
handleChange();
}
};
if (pointer.initialized) {
return [pointer.get(), setFn];
}
const value =
typeof defaultValue === 'function' ? (defaultValue as () => Value)() : defaultValue;
pointer.set(value);
return [value, setFn];
});
}