Better handle falsy values in Select & GraphQL cells

Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
Émile Ré
2025-12-02 10:47:40 +04:00
parent 57e267afe0
commit 519dd716b3
5 changed files with 17 additions and 13 deletions

View File

@@ -4,14 +4,18 @@ import { useCallback, useRef, useState } from "react";
* A useState hook that also returns a ref to the current state (usable in callbacks)
*/
export function useStateWithRef<T>(initialValue: T) {
const [state, setState] = useState(initialValue);
const [state, setState] = useState<T>(initialValue);
const ref = useRef(state);
return [
state,
useCallback((v: T) => {
setState(v);
ref.current = v;
useCallback((v: T | ((prevState: T) => T)) => {
setState(prev => {
const nextState = typeof v === "function"
? (v as (prevState: T) => T)(prev) : v;
ref.current = nextState;
return nextState;
});
}, []),
ref,
] as const;