-
-
Notifications
You must be signed in to change notification settings - Fork 36
Open
Description
Love the module, but I was running into stale state issues.
This might be a useful addition:
const useStateRefWithCallbackLazy = initialValue => {
const valueRef = useRef(initialValue);
const callbackRef = useRef(null);
const [value, setValue] = useState(initialValue);
useEffect(() => {
if (callbackRef.current) {
callbackRef.current();
callbackRef.current = null;
}
}, [value]);
const setValueWithCallback = (newValue, callback) => {
valueRef.current = newValue;
callbackRef.current = callback;
return setValue(newValue);
};
return [value, valueRef, setValueWithCallback];
};
Metadata
Metadata
Assignees
Labels
No labels