Better handle falsy values in Select & GraphQL cells
Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user