antd-mobile
Version:
<div align="center">
23 lines • 828 B
JavaScript
import { useRef } from 'react';
import { useMemoizedFn, useUpdate } from 'ahooks';
export function usePropsValue(options) {
const {
value,
defaultValue,
onChange
} = options;
const update = useUpdate();
const stateRef = useRef(value !== undefined ? value : defaultValue);
if (value !== undefined) {
stateRef.current = value;
}
const setState = useMemoizedFn((v, forceTrigger = false) => {
// `forceTrigger` means trigger `onChange` even if `v` is the same as `stateRef.current`
const nextValue = typeof v === 'function' ? v(stateRef.current) : v;
if (!forceTrigger && nextValue === stateRef.current) return;
stateRef.current = nextValue;
update();
return onChange === null || onChange === void 0 ? void 0 : onChange(nextValue);
});
return [stateRef.current, setState];
}