Files
video-gen/video-gen-admin/node_modules/@rc-component/tabs/es/hooks/useSyncState.js
T
2026-05-25 17:08:18 +08:00

14 lines
498 B
JavaScript

import * as React from 'react';
export default function useSyncState(defaultState, onChange) {
const stateRef = React.useRef(defaultState);
const [, forceUpdate] = React.useState({});
function setState(updater) {
const newValue = typeof updater === 'function' ? updater(stateRef.current) : updater;
if (newValue !== stateRef.current) {
onChange(newValue, stateRef.current);
}
stateRef.current = newValue;
forceUpdate({});
}
return [stateRef.current, setState];
}