Skip to content

Instantly share code, notes, and snippets.

@lisonge
Created August 22, 2025 06:30
Show Gist options
  • Save lisonge/c2506767ac3ec6b97c3ec4017c53a836 to your computer and use it in GitHub Desktop.
Save lisonge/c2506767ac3ec6b97c3ec4017c53a836 to your computer and use it in GitHub Desktop.
import { isEqual } from 'lodash-es'
import { customRef, computed, type ShallowRef } from 'vue'
export const useEqualRef = <T>(initValue: T): ShallowRef<T> => {
return customRef((track, trigger) => {
let value = initValue
return {
get() {
track()
return value
},
set(newValue) {
if (!isEqual(value, newValue)) {
value = newValue
trigger()
}
}
}
})
}
const useEqualComputed = <T>(getter: () => T): ComputedRef<T> => {
let value: T
return computed(() => {
const newValue = getter()
if (!isEqual(value, newValue)) {
value = newValue
}
return value
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment