vue-hooks-plus
Version:
Vue hooks library
17 lines (16 loc) • 443 B
JavaScript
import { ref, watchEffect, readonly } from "vue";
const defaultShouldUpdate = (a, b) => !Object.is(a, b);
function usePrevious(state, shouldUpdate = defaultShouldUpdate) {
const prevRef = ref();
const curRef = ref();
watchEffect(() => {
if (shouldUpdate(curRef.value, state.value)) {
prevRef.value = curRef.value;
curRef.value = state.value;
}
});
return readonly(prevRef);
}
export {
usePrevious as default
};