vue-hooks-plus
Version:
Vue hooks library
16 lines (15 loc) • 444 B
JavaScript
const vue = require("vue");
const defaultShouldUpdate = (a, b) => !Object.is(a, b);
function usePrevious(state, shouldUpdate = defaultShouldUpdate) {
const prevRef = vue.ref();
const curRef = vue.ref();
vue.watchEffect(() => {
if (shouldUpdate(curRef.value, state.value)) {
prevRef.value = curRef.value;
curRef.value = state.value;
}
});
return vue.readonly(prevRef);
}
module.exports = usePrevious;
;