ရိုးရိုးလေး ရှင်းပြမယ်
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 တည်ဆောက်နည်းကို လေ့လာမည်။
- Reactive source
- Dependency tracking
- Updated value / UI
Primitive state အတွက် ref သုံးရန်
အဓိက Syntax / Pattern
<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 ကို အဆင့်လိုက်ဖတ်မယ်
- Reactive source
<script setup>ref() သည် တန်ဖိုးတစ်ခုကို reactive wrapper ထဲထည့်ပြီး JavaScript တွင် .value ဖြင့်ဖတ်/ရေးရသည်။ reactive() သည် object ကို proxy အဖြစ်ပြောင်းပေးသည်။ Template ထဲတွင် ref ကို .value မလိုဘဲ တိုက်ရိုက်သုံးနိုင်သည်။
- Dependency tracking
user.points += 5ref() နှင့် reactive() ဖြင့် UI ပြောင်းလဲစေသော reactive state တည်ဆောက်နည်းကို လေ့လာမည်။
- Updated value / UI
</template>Reactivity with ref() and reactive() ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
- Reactive source
- Dependency tracking
- Updated value / UI
Success
Primitive state အတွက် ref သုံးရန်
Failure
Script ထဲတွင် ref ကို .value မပါဘဲ assign လုပ်ခြင်း
ရိုးရိုးလေး ရှင်းပြမယ်
ref() သည် တန်ဖိုးတစ်ခုကို reactive wrapper ထဲထည့်ပြီး JavaScript တွင် .value ဖြင့်ဖတ်/ရေးရသည်။ reactive() သည် object ကို proxy အဖြစ်ပြောင်းပေးသည်။ Template ထဲတွင် ref ကို .value မလိုဘဲ တိုက်ရိုက်သုံးနိုင်သည်။
<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>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 ကိုခန့်မှန်းပါ
<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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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