Thuta Learning
ရှာဖွေရန်
BasicMobile Developmentintermediate

Styling with StyleSheet

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

  • Styling with StyleSheet ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် code ရေးပြီး Expo Go ပေါ်မှာ run ကြည့်တတ်မယ်
  • Real app project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

StyleSheet.create() က style object တွေကို validate/optimize လုပ်ပေးတဲ့ helper function ပါ — plain object ({ }) ကို တိုက်ရိုက်သုံးလည်း အလုပ်ဖြစ်ပေမယ့် StyleSheet.create ကို သုံးတာ performance ပိုကောင်းပြီး style error ကို development mode မှာ warning ပြပေးပါတယ်။ Property name တွေက CSS နဲ့ ဆင်တူပေမယ် camelCase ဖြစ်ပါတယ် (background-color → backgroundColor), unit မလိုအပ်ပါဘူး (px မထည့်ရ, number ချည်း ရေးပါတယ်)။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Style object များစွာကို component တစ်ခုမှာ ပေါင်းသုံးချင်ရင် array syntax သုံးလို့ရပါတယ် — style={[styles.base, styles.active]} ဆိုရင် base style အရင်သုံးပြီး active style ကို override လုပ်ပါတယ် (array ရဲ့ နောက်ဆုံး item က priority အမြင့်ဆုံး)။ Conditional styling (ဥပမာ button pressed state) ကို ဒီ array pattern နဲ့ လွယ်လွယ်ကူကူ implement လုပ်နိုင်ပါတယ်။

Code နမူနာ

javascript
import { StyleSheet, View, Text } from 'react-native';

const styles = StyleSheet.create({
  card: {
    backgroundColor: '#f0f0f0',
    borderRadius: 12,
    padding: 16,
    marginBottom: 8,
  },
  title: {
    fontSize: 18,
    fontWeight: '600',
    color: '#1a1a2e',
  },
});

// Combining styles conditionally
<View style={[styles.card, isActive && styles.activeCard]}>
  <Text style={styles.title}>Card Title</Text>
</View>
You should see
Card component တစ်ခုကို StyleSheet.create နဲ့ style ချနိုင်ပြီး conditional style ကို array syntax နဲ့ ပေါင်းသုံးနိုင်မည်။

၅ မိနစ် စမ်းကြည့်

Card style (backgroundColor, borderRadius, padding) ကို ကိုယ်တိုင် ရေးပြီး, isActive true ဖြစ်ရင် border color ပြောင်းတဲ့ conditional style ထပ်ထည့်ကြည့်ပါ။

သတိလေးတစ်ချက်

Style property အားလုံးက CSS နဲ့ တူတယ်လို့ မယူဆပါနှင့် — 'display: flex' ကို default အနေနဲ့ View တိုင်းမှာ ရှိပြီးသားပါ, ဒါပေမယ့် flexDirection default က 'column' ပါ (CSS web ရဲ့ 'row' default နဲ့ ကွဲပါတယ်)။

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

  • CSS unit (px, em) ကို React Native style ထဲ ထည့်ရေးမိခြင်း — number ချည်းသာ ရေးရပါတယ်
  • kebab-case (background-color) ကို camelCase အစား ရေးမိခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

Card style (backgroundColor, borderRadius, padding) ကို ကိုယ်တိုင် ရေးပြီး, isActive true ဖြစ်ရင် border color ပြောင်းတဲ့ conditional style ထပ်ထည့်ကြည့်ပါ။

You'll know it worked when: Card component တစ်ခုကို StyleSheet.create နဲ့ style ချနိုင်ပြီး conditional style ကို array syntax နဲ့ ပေါင်းသုံးနိုင်မည်။

Styling with StyleSheet | Thuta Learning