Thuta Learning
React Native
BasicMobile Developmentအလယ်အလတ်

Styling with StyleSheet

React Nativeသင်ခန်းစာ 520 မိနစ်

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

  • Styling with StyleSheet ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
  • Accessibility, failure နဲ့ lifecycle cases စမ်းနိုင်ရန်
  • Maintainable, testable နဲ့ performant React Native code ရေးနိုင်ရန်

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

Styling with StyleSheet ကို ရိုးရိုးလေးနားလည်မယ်

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 ချည်း ရေးပါတယ်)။

Problem → Why → Solution

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

CSS unit (px, em) ကို React Native style ထဲ ထည့်ရေးမိခြင်း — number ချည်းသာ ရေးရပါတယ်

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

kebab-case (background-color) ကို camelCase အစား ရေးမိခြင်း

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

Styling with StyleSheet ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။

Mental Model
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Styling with StyleSheet ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

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>

Styling with StyleSheet ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။

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

  1. JavaScript stateimport { StyleSheet, View, Text } from 'react-native';

    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 ချည်း ရေးပါတယ်)။

  2. Native component + layoutfontSize: 18,

    Styling with StyleSheet ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။

  3. Android / iOS UI</View>

    Styling with StyleSheet example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Execution Flow
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Success

Styling with StyleSheet ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

Failure

CSS unit (px, em) ကို React Native style ထဲ ထည့်ရေးမိခြင်း — number ချည်းသာ ရေးရပါတယ်

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

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 နဲ့ ကွဲပါတယ်)။

Real-world usage

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

Styling with StyleSheet example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Mini challenge

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

Styling with StyleSheet example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
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>

ရလဒ်: Styling with StyleSheet ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

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

Styling with StyleSheet example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Quick Quiz 1 / 3

Styling with StyleSheet ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • JavaScript state → Native component + layout → Android / iOS UI
  • Styling with StyleSheet ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။
  • CSS unit (px, em) ကို React Native style ထဲ ထည့်ရေးမိခြင်း — number ချည်းသာ ရေးရပါတယ်
  • Styling with StyleSheet example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

React Native Documentation — React Native

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

  • CSS unit (px, em) ကို React Native style ထဲ ထည့်ရေးမိခြင်း — number ချည်းသာ ရေးရပါတယ်
  • kebab-case (background-color) ကို camelCase အစား ရေးမိခြင်း
  • Simulator တစ်ခု၏ happy path မှန်ရုံဖြင့် real devices, release builds, permissions, accessibility, offline နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

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

Styling with StyleSheet example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Card component တစ်ခုကို StyleSheet.create နဲ့ style ချနိုင်ပြီး conditional style ကို array syntax နဲ့ ပေါင်းသုံးနိုင်မည်။

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

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

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

Styling with StyleSheet | Thuta Learning