Keep reactivity with toRefs
Destructuring a reactive object looks tidy, but it breaks the link. The
destructured variables are plain values, so changing the object no longer updates
them:
const state = reactive({ count: 0 });
const { count } = state; // count is now a plain 0
toRefs converts each property into a ref that stays connected to the source:
<script setup>
import { reactive, toRefs } from 'vue';
const state = reactive({ count: 0 });
const { count } = toRefs(state);
</script>
<template>
<button @click="state.count++">Count: {{ count }}</button>
</template>
Now count is a ref, so {{ count }} unwraps it in the template and it tracks
state.count. Write through either one — state.count or count.value — and
the other sees the change.
Use toRefs when you want the convenience of destructuring without losing
reactivity. For a single property, toRef(state, 'count') returns a ref for that
one property.
The Test for this Lesson mounts your component, clicks the button once, and
checks the label. Your component is exported from App.vue.
Challenge
The button updates state.count, but the label never follows because the count
was destructured as a plain value. Keep it reactive with toRefs: export an
App component from App.vue that renders a button labelled Count: 0 and
adds one to state.count on each click.
The Test mounts your component, clicks the button once, and expects the label to
read Count: 1. The starter destructures the count without toRefs, so the
label stays Count: 0 and the Test fails.