การสร้าง StyleSheet
React Native ใช้ StyleSheet ในการกำหนดสไตล์ที่คล้าย CSS แต่มีโครงสร้างและวิธีการใช้งานที่เหมาะสมกับ Mobile Development
การสร้าง StyleSheet:
- ใช้ 
StyleSheet.create()เพื่อสร้างสไตล์ - ออกแบบให้โค้ดสไตล์แยกออกจาก UI เพื่อความชัดเจน
 
ตัวอย่าง
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const App = () => {
  return (
    <View style={styles.container}>
      <Text style={styles.title}>Hello, React Native!</Text>
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#f0f0f0',
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
    color: '#333',
  },
});
export default App;
คำอธิบาย:
container: กำหนด Layout ของแอป เช่น การจัดวางองค์ประกอบกลางหน้าจอtitle: สไตล์ข้อความ เช่น ขนาดตัวอักษรและสี
การจัดการ Layout ด้วย Flexbox
React Native ใช้ Flexbox สำหรับจัดการ Layout เพื่อให้ทำงานได้เหมือน CSS Flexbox
คุณสมบัติสำคัญของ Flexbox:
flex: กำหนดขนาดหรือการเติบโตขององค์ประกอบflexDirection: ระบุการจัดเรียง (rowหรือcolumn)justifyContent: จัดวางองค์ประกอบในแกนหลัก (main axis)alignItems: จัดวางองค์ประกอบในแกนรอง (cross axis)
ตัวอย่าง:
import React from 'react';
import { View, StyleSheet } from 'react-native';
const FlexboxExample = () => {
  return (
    <View style={styles.container}>
      <View style={styles.box1} />
      <View style={styles.box2} />
      <View style={styles.box3} />
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: '#e0e0e0',
  },
  box1: {
    width: 50,
    height: 50,
    backgroundColor: '#ff5733',
  },
  box2: {
    width: 50,
    height: 50,
    backgroundColor: '#33ff57',
  },
  box3: {
    width: 50,
    height: 50,
    backgroundColor: '#3357ff',
  },
});
export default FlexboxExample;
คำอธิบาย:
flexDirection: 'row': จัดวางองค์ประกอบในแนวนอนjustifyContent: 'space-around': กระจายองค์ประกอบให้มีระยะห่างเท่ากันalignItems: 'center': จัดวางองค์ประกอบให้อยู่ตรงกลางในแกนรอง
การใช้งาน Dimensions และ Platform
 Dimensions:
ใช้สำหรับดึงข้อมูลขนาดหน้าจอ เช่น ความกว้างและความสูง
ตัวอย่าง:
import React from 'react';
import { View, Text, Dimensions, StyleSheet } from 'react-native';
const { width, height } = Dimensions.get('window');
const DimensionsExample = () => {
  return (
    <View style={styles.container}>
      <Text>Width: {width}px</Text>
      <Text>Height: {height}px</Text>
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});
export default DimensionsExample;
คำอธิบาย:
Dimensions.get('window'): คืนค่าความกว้างและความสูงของหน้าจอ
Platform:
ใช้เพื่อจัดการพฤติกรรมแอปที่แตกต่างกันระหว่าง iOS และ Android
ตัวอย่าง:
import React from 'react';
import { View, Text, Platform, StyleSheet } from 'react-native';
const PlatformExample = () => {
  return (
    <View style={styles.container}>
      <Text>
        Running on: {Platform.OS === 'ios' ? 'iOS' : 'Android'}
      </Text>
      <Text>Version: {Platform.Version}</Text>
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});
export default PlatformExample;
คำอธิบาย:
Platform.OS: ตรวจสอบว่ากำลังรันบน iOS หรือ AndroidPlatform.Version: แสดงเวอร์ชันของระบบปฏิบัติการ
ข้อดีของการใช้งาน StyleSheet:
- โค้ดสไตล์มีระเบียบและอ่านง่าย
 - สนับสนุนการใช้งาน Flexbox สำหรับจัด Layout
 - รองรับการพัฒนาแอปที่ทำงานได้ทั้ง iOS และ Android