Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Managing State with ref and reactive | Section
/
Vue.js Fundamentals and App Development

bookManaging State with ref and reactive

Sveip for å vise menyen

In Vue, application state represents the data that controls your interface. Vue provides ref() and reactive() to create and manage this state.

Use ref() for simple values like numbers or strings.

<script setup>
import { ref } from "vue";

const count = ref(0);
</script>

<template>
  <p>{{ count }}</p>
</template>

Use reactive() for objects with multiple properties.

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

const user = reactive({
  name: "John",
  age: 25
});
</script>

<template>
  <p>{{ user.name }}</p>
  <p>{{ user.age }}</p>
</template>

To update values, use .value with ref() and direct property access with reactive().

count.value++;
user.name = "Emily";

Both ref() and reactive() create reactive state that automatically updates the UI when data changes.

question mark

When should you use reactive() in Vue?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 13

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 13
some-alt