Hooki cyklu życia pozwalają uruchamiać kod w określonych momentach życia komponentu — tworzenie, montowanie do DOM, aktualizacja i usunięcie. W Composition API to funkcje wywoływane wewnątrz setup.
Główne hooki w kolejności
<script setup>
import { onMounted, onUpdated, onUnmounted, ref } from "vue";
const data = ref(null);
onMounted(() => {
// component is now in the DOM — fetch data, access elements, init libraries
fetchData().then(d => (data.value = d));
});
onUpdated(() => {
// runs after the DOM re-renders due to a reactive change
});
onUnmounted(() => {
// component is being removed — CLEAN UP here
clearInterval(timer);
window.removeEventListener("resize", handler);
});
</script>
