unis_crm/frontend1/node_modules/@rc-component/util/es/hooks/useSyncState.js

20 lines
758 B
JavaScript
Raw Normal View History

2026-03-26 09:29:55 +00:00
import * as React from 'react';
import useEvent from "./useEvent";
/**
* Same as React.useState but will always get latest state.
* This is useful when React merge multiple state updates into one.
* e.g. onTransitionEnd trigger multiple event at once will be merged state update in React.
*/
function useSyncState(defaultValue) {
const [, forceUpdate] = React.useReducer(x => x + 1, 0);
const currentValueRef = React.useRef(defaultValue);
const getValue = useEvent(() => {
return currentValueRef.current;
});
const setValue = useEvent(updater => {
currentValueRef.current = typeof updater === 'function' ? updater(currentValueRef.current) : updater;
forceUpdate();
});
return [getValue, setValue];
}
export default useSyncState;