soda-material
Version:
A React(>=18) component library that may follow [Material Design 3](https://m3.material.io/components) (a.k.a. Material You)
27 lines (25 loc) • 702 B
text/typescript
/**
* Use this to avoid react's immutable ref:
*
* ```ts
* interface RefObject<T> {
* readonly current: T | null;
* }
* // Bivariance hack for consistent unsoundness with RefObject
* type RefCallback<T> = { bivarianceHack(instance: T | null): void }["bivarianceHack"];
* type Ref<T> = RefCallback<T> | RefObject<T> | null;
* type LegacyRef<T> = string | Ref<T>;
* ```
*/
export type ReactRef<T> =
| React.RefCallback<T>
| React.MutableRefObject<T>
| null
/**
* Support callback and object.current
*/
export function setReactRef<T>(ref: ReactRef<T> | undefined, value: T) {
if (!ref) return
if (typeof ref === 'function') ref(value)
else ref.current = value
}