1.0.1 โข Published 9 months ago
@daveyrojo/use-ref-with-reset v1.0.1
use-ref-with-reset
๐ก A tiny Vue 3 composable that tracks and resets reactive values to their original state โ with optional deep watching, prop syncing, and helper methods like
resetTo()andset().
โจ Features
- โ Tracks changes and compares against an original reference
- ๐
reset()support to roll back state - ๐ง
isModifiedto know if value has changed - ๐
resetTo()to redefine the original value - ๐ง
set()for safe, shallow updates - ๐ Optional deep watching
- ๐ฆ Lightweight and tree-shakeable
๐ฆ Installation
npm install use-ref-with-reset
# or
pnpm add use-ref-with-reset๐งช Quick Example
import { useRefWithReset } from 'use-ref-with-reset'
const { value, reset, isModified, set, resetTo } = useRefWithReset({ count: 1 })
value.value.count++ // Modify
console.log(isModified.value) // true
reset() // Back to { count: 1 }
resetTo({ count: 100 }) // Redefine original
set({ count: 999 }) // Change value๐ง Usage with Props
const props = defineProps<{
config: { visible: boolean }
}>()
const {
value: config,
original,
reset,
isModified
} = useRefWithReset(() => props.config, {
watchSource: () => props.config,
deepWatch: true
})๐งฐ API
useRefWithReset(initial, options)
argument,type,Description
initial, T | () => T | Ref<T>, The initial or reactive value
options, UseRefWithResetOptions<T>, Options for deep watch, syncing, etc.๐งฉ Options
interface UseRefWithResetOptions<T> {
watchSource?: MaybeRefOrGetter<T>
syncOriginalOnSourceChange?: boolean // default: true
deepWatch?: boolean // default: false
freezeOriginal?: boolean // default: false
}โ When to Use
Use Case, โ
Recommended?
Temporary form state, โ
Yes
Compare props to modified local state, โ
Yes
Undo history, โ No
Complex object diffs, โ ๏ธ Better with deep libraries like fast-deep-equal๐งช Testing
npm run testIncludes a Vitest test suite in /test
๐งโ๐ป Author
Made with โค๏ธ by David Eldridge
๐ License
MIT