Implement a component that listens to window resize events and updates its data.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
<template>
<p>Width: {{ width }}</p>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
const width = ref(window.innerWidth);
const onResize = () => { width.value = window.innerWidth; };
onMounted(() => window.addEventListener('resize', onResize));
onUnmounted(() => window.removeEventListener('resize', onResize));
</script>