Created
August 22, 2021 16:57
-
-
Save bacarybruno/fbe3ec4df3c9346fbaf1ad9946764ae8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ref, watch, onBeforeUnmount } from 'vue'; | |
export const useState = (initialValue) => { | |
const state = ref(initialValue); | |
const setState = (value) => { | |
state.value = value; | |
}; | |
return [state, setState]; | |
}; | |
export const useEffect = (callback = () => {}, deps = []) => { | |
let cleanup = () => {}; | |
if (deps.length === 0) { | |
cleanup = callback(); | |
} | |
deps.forEach((dep) => watch(dep, () => { | |
cleanup = callback(); | |
})); | |
onBeforeUnmount(() => { | |
if (typeof cleanup === 'function') { | |
cleanup(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example :
Bare VueJS