Composable with lifecycle hooks
📖 Scenario: You are building a Vue 3 app that needs to track the window width and update it reactively when the window is resized.
🎯 Goal: Create a composable function that uses Vue lifecycle hooks to track and update the window width reactively.
📋 What You'll Learn
Create a reactive variable to store the window width
Use the
onMounted lifecycle hook to add a window resize event listenerUse the
onUnmounted lifecycle hook to remove the event listenerReturn the reactive window width variable from the composable
💡 Why This Matters
🌍 Real World
Tracking window size is common in responsive design to adapt UI elements dynamically.
💼 Career
Understanding composables and lifecycle hooks is essential for building maintainable Vue 3 applications.
Progress0 / 4 steps