What if we made something like this? (Itâs just an idea, so I havenât verified it properly yet.) typescript
function useGetter<T>(init_value : T){
let [state, update] = useState<T>(init_value);
let ref = useRef(state);
const getter = () => {
return ref.current;
}
const setter = (f : Function) => {
let newState = f(ref.current);
newState = Object.assign({}, newState); // force update
ref.current = newState;
update(newState);
}
return [getter, setter];
}
This page is auto-translated from [/nishio/React Hooksă§getter](https://scrapbox.io/nishio/React Hooksă§getter) using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iâm very happy to spread my thought to non-Japanese readers.