Vue3 Fundamentals · Reactivity

Group state with reactive

reactive

Group state with reactive

A ref holds one value. When several values belong together — a count and the name beside it — reaching for a ref each time gets noisy. reactive turns a whole object into reactive state, so every property is tracked.

<script setup>
import { reactive } from 'vue';

const state = reactive({ count: 0, name: 'Vue' });
</script>

<template>
  <button @click="state.count++">Count: {{ state.count }}</button>
</template>

reactive returns a proxy of the object you pass. Reading a property through the proxy records it; writing one triggers a re-render. You keep the object's shape, so you reach a value with state.count — no .value in sight.

That is the trade: reactive groups related state under one name, while ref wraps a single value. Use reactive when the values form a unit, and reach for ref when one value stands alone.

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 shows Count: 0 and never moves. Group the count in a reactive object so Vue re-renders when it changes: export an App component from App.vue that renders a button labelled Count: 0 and adds one to the count on each click.

The Test mounts your component, clicks the button once, and expects the label to read Count: 1. The starter keeps the count in a plain object, so the label never updates and the Test fails.

App.vue
<script setup>
const state = { count: 0 };
</script>

<template>
  <button @click="state.count++">Count: {{ state.count }}</button>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.