Thuta Learning
Vue.js
BasicWeb Developmentအခြေခံ

Reactivity with ref() and reactive()

Vue.jsသင်ခန်းစာ 520 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Primitive state အတွက် ref သုံးရန်
  • Object state အတွက် reactive သုံးရန်
  • Template auto-unwrapping ကိုနားလည်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Reactivity with ref() and reactive() ကို ရိုးရိုးလေးနားလည်မယ်

ref() သည် တန်ဖိုးတစ်ခုကို reactive wrapper ထဲထည့်ပြီး JavaScript တွင် .value ဖြင့်ဖတ်/ရေးရသည်။ reactive() သည် object ကို proxy အဖြစ်ပြောင်းပေးသည်။ Template ထဲတွင် ref ကို .value မလိုဘဲ တိုက်ရိုက်သုံးနိုင်သည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Script ထဲတွင် ref ကို .value မပါဘဲ assign လုပ်ခြင်း

ဖြစ်လေ့ရှိတဲ့အမှား

reactive object ကို object အသစ်ဖြင့် တိုက်ရိုက်အစားထိုးခြင်း

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

ref() နှင့် reactive() ဖြင့် UI ပြောင်းလဲစေသော reactive state တည်ဆောက်နည်းကို လေ့လာမည်။

Mental Model
  1. Reactive source
  2. Dependency tracking
  3. Updated value / UI

Primitive state အတွက် ref သုံးရန်

အဓိက Syntax / Pattern

vue
<script setup>
import { ref, reactive } from 'vue'

const count = ref(0)
const user = reactive({ name: 'Aye Aye', points: 10 })

function addPoint() {
  count.value++
  user.points += 5
}
</script>

<template>
  <button @click="addPoint">Clicks: {{ count }}</button>
  <p>{{ user.name }} — {{ user.points }} points</p>
</template>

ref() နှင့် reactive() ဖြင့် UI ပြောင်းလဲစေသော reactive state တည်ဆောက်နည်းကို လေ့လာမည်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Reactive source<script setup>

    ref() သည် တန်ဖိုးတစ်ခုကို reactive wrapper ထဲထည့်ပြီး JavaScript တွင် .value ဖြင့်ဖတ်/ရေးရသည်။ reactive() သည် object ကို proxy အဖြစ်ပြောင်းပေးသည်။ Template ထဲတွင် ref ကို .value မလိုဘဲ တိုက်ရိုက်သုံးနိုင်သည်။

  2. Dependency trackinguser.points += 5

    ref() နှင့် reactive() ဖြင့် UI ပြောင်းလဲစေသော reactive state တည်ဆောက်နည်းကို လေ့လာမည်။

  3. Updated value / UI</template>

    Reactivity with ref() and reactive() ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Execution Flow
  1. Reactive source
  2. Dependency tracking
  3. Updated value / UI

Success

Primitive state အတွက် ref သုံးရန်

Failure

Script ထဲတွင် ref ကို .value မပါဘဲ assign လုပ်ခြင်း

ရိုးရိုးလေး ရှင်းပြမယ်

ref() သည် တန်ဖိုးတစ်ခုကို reactive wrapper ထဲထည့်ပြီး JavaScript တွင် .value ဖြင့်ဖတ်/ရေးရသည်။ reactive() သည် object ကို proxy အဖြစ်ပြောင်းပေးသည်။ Template ထဲတွင် ref ကို .value မလိုဘဲ တိုက်ရိုက်သုံးနိုင်သည်။

vue
<script setup>
import { ref, reactive } from 'vue'

const count = ref(0)
const user = reactive({ name: 'Aye Aye', points: 10 })

function addPoint() {
  count.value++
  user.points += 5
}
</script>

<template>
  <button @click="addPoint">Clicks: {{ count }}</button>
  <p>{{ user.name }} — {{ user.points }} points</p>
</template>
You should see
Clicks: 1
Aye Aye — 15 points

လက်တွေ့စမ်းကြည့်ရန်

Shopping cart quantity ကို ref ဖြင့်သိမ်းပြီး +/− button နှစ်ခုဖြင့် ပြောင်းလဲပါ။

Reactivity Fundamentals — Vue.js

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Reactivity with ref() and reactive() ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Shopping cart quantity ကို ref ဖြင့်သိမ်းပြီး +/− button နှစ်ခုဖြင့် ပြောင်းလဲပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
vue
<script setup>
import { ref, reactive } from 'vue'

const count = ref(0)
const user = reactive({ name: 'Aye Aye', points: 10 })

function addPoint() {
  count.value++
  user.points += 5
}
</script>

<template>
  <button @click="addPoint">Clicks: {{ count }}</button>
  <p>{{ user.name }} — {{ user.points }} points</p>
</template>

ရလဒ်: Primitive state အတွက် ref သုံးရန်

အရိပ်အမြွက် ကြည့်ရန်

Reactivity with ref() and reactive() ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Quick Quiz 1 / 3

Reactivity with ref() and reactive() ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Reactive source → Dependency tracking → Updated value / UI
  • ref() နှင့် reactive() ဖြင့် UI ပြောင်းလဲစေသော reactive state တည်ဆောက်နည်းကို လေ့လာမည်။
  • Script ထဲတွင် ref ကို .value မပါဘဲ assign လုပ်ခြင်း
  • Reactivity with ref() and reactive() ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Reactivity Fundamentals — Vue.js

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Script ထဲတွင် ref ကို .value မပါဘဲ assign လုပ်ခြင်း
  • reactive object ကို object အသစ်ဖြင့် တိုက်ရိုက်အစားထိုးခြင်း

လေ့ကျင့်ခန်း

Shopping cart quantity ကို ref ဖြင့်သိမ်းပြီး +/− button နှစ်ခုဖြင့် ပြောင်းလဲပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Clicks: 1 Aye Aye — 15 points

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Reactivity with ref() and reactive() | Thuta Learning