11 lines
395 B
TypeScript
11 lines
395 B
TypeScript
import { Dispatch, SetStateAction, useState } from "react";
|
|
|
|
export function useProxyState<T>(initial: T, setter: (value: T) => void): [T, Dispatch<SetStateAction<T>>] {
|
|
const [value, proxy] = useState<T>(initial);
|
|
|
|
return [value, (newValue: SetStateAction<T>) => {
|
|
proxy(newValue);
|
|
setter(typeof newValue === "function" ? (newValue as any)(value) : newValue);
|
|
}];
|
|
}
|