import type { Ref } from 'vue'; import { ref } from 'vue'; type Updater = (prev: T) => T; export default function useSyncState( defaultState: T, onChange: (newValue: T, prevValue: T) => void, ): [Ref, (updater: T | Updater) => void] { const stateRef = ref(defaultState); function setState(updater: any) { const newValue = typeof updater === 'function' ? updater(stateRef.value) : updater; if (newValue !== stateRef.value) { onChange(newValue, stateRef.value as T); } stateRef.value = newValue; } return [stateRef as Ref, setState]; }