ရိုးရိုးလေး ရှင်းပြမယ်
State Management with Pinia ကို ရိုးရိုးလေးနားလည်မယ်
Local component state ကို အလွယ်တကူမရွှေ့ပါနှင့်။ User session, cart သို့မဟုတ် app-wide filters ကဲ့သို့ component များစွာမျှဝေသည့် state အတွက် Pinia သုံးပါ။ State ကို function ဖြင့်ကြေညာပြီး derived state ကို getters, changes ကို actions အဖြစ်ရေးပါ။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
App တွင် Pinia plugin register မလုပ်ခြင်း
ဖြစ်လေ့ရှိတဲ့အမှား
Component-local state အားလုံးကို global store ထဲထည့်ခြင်း
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။
- Action / shared data
- Reactive store
- Subscribed component
Pinia store တည်ဆောက်ရန်
အဓိက Syntax / Pattern
import { defineStore } from 'pinia'
export const useCartStore = defineStore('cart', {
state: () => ({ items: [] }),
getters: {
totalItems: (state) => state.items.length
},
actions: {
add(product) {
this.items.push(product)
}
}
})Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Action / shared data
import { defineStore } from 'pinia'Local component state ကို အလွယ်တကူမရွှေ့ပါနှင့်။ User session, cart သို့မဟုတ် app-wide filters ကဲ့သို့ component များစွာမျှဝေသည့် state အတွက် Pinia သုံးပါ။ State ကို function ဖြင့်ကြေညာပြီး derived state ကို getters, changes ကို actions အဖြစ်ရေးပါ။
- Reactive store
actions: {Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။
- Subscribed component
})State Management with Pinia ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
- Action / shared data
- Reactive store
- Subscribed component
Success
Pinia store တည်ဆောက်ရန်
Failure
App တွင် Pinia plugin register မလုပ်ခြင်း
ရိုးရိုးလေး ရှင်းပြမယ်
Local component state ကို အလွယ်တကူမရွှေ့ပါနှင့်။ User session, cart သို့မဟုတ် app-wide filters ကဲ့သို့ component များစွာမျှဝေသည့် state အတွက် Pinia သုံးပါ။ State ကို function ဖြင့်ကြေညာပြီး derived state ကို getters, changes ကို actions အဖြစ်ရေးပါ။
import { defineStore } from 'pinia'
export const useCartStore = defineStore('cart', {
state: () => ({ items: [] }),
getters: {
totalItems: (state) => state.items.length
},
actions: {
add(product) {
this.items.push(product)
}
}
})cart.totalItems = 0လက်တွေ့စမ်းကြည့်ရန်
User preferences အတွက် theme နှင့် locale state ပါသော Pinia store တစ်ခုတည်ဆောက်ပါ။
Pinia State — Pinia
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
State Management with Pinia ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
User preferences အတွက် theme နှင့် locale state ပါသော Pinia store တစ်ခုတည်ဆောက်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
import { defineStore } from 'pinia'
export const useCartStore = defineStore('cart', {
state: () => ({ items: [] }),
getters: {
totalItems: (state) => state.items.length
},
actions: {
add(product) {
this.items.push(product)
}
}
})ရလဒ်: Pinia store တည်ဆောက်ရန်
အရိပ်အမြွက် ကြည့်ရန်
State Management with Pinia ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Action / shared data → Reactive store → Subscribed component
- Component များစွာမျှဝေသုံးသော state ကို Pinia store ဖြင့် စနစ်တကျစီမံမည်။
- App တွင် Pinia plugin register မလုပ်ခြင်း
- State Management with Pinia ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
Pinia State — Pinia