ทำไมต้องใช้ Plugins ใน Expo (React Native)

ทำไมต้องใช้ Plugins ใน Expo?

Expo ไม่ได้แก้ไข native code โดยอัตโนมัติ

  • Expo ใช้ Managed Workflow ที่ไม่ให้แก้ไขไฟล์ AndroidManifest.xml หรือ Info.plist ตรง ๆ
  • บางไลบรารีต้องมีการตั้งค่า permissions, native dependencies, หรือ native modules
  • บางไลบรารีต้องการ native configuration เช่น Bluetooth, Camera หรือ Background Services

🛠 ตัวอย่างไลบรารีที่ต้องใช้ Plugins

1. react-native-vision-camera

📌 เป็นไลบรารีที่ต้องใช้ native permissions และ native code

yarn add react-native-vision-camera

จากนั้นต้องเพิ่ม plugins ใน expo.config.js:

export default {
  plugins: ["react-native-vision-camera"],
};

2. react-native-reanimated

📌 เป็นไลบรารีที่ต้องใช้ JSI (JavaScript Interface) และต้องเพิ่ม Babel Plugin

yarn add react-native-reanimated

เพิ่ม plugins ใน expo.config.js:

export default {
  plugins: ["react-native-reanimated/plugin"],
};

และเพิ่มใน babel.config.js:

module.exports = {
  presets: ["babel-preset-expo"],
  plugins: ["react-native-reanimated/plugin"],
};

3. expo-location

📌 เป็นไลบรารีที่ใช้ GPS และต้องขอ permissions ใน native

yarn add expo-location

จากนั้นต้องเพิ่ม plugins:

export default {
  plugins: [
    ["expo-location", {
      locationAlwaysAndWhenInUsePermission: "แอปต้องการเข้าถึงตำแหน่งของคุณ",
    }],
  ],
};

🔥 Expo Plugins ช่วยอะไรบ้าง?

  • ✅ ไม่ต้อง Eject -> ใช้ไลบรารี native ได้โดยไม่ต้องเปลี่ยนไปใช้ Bare Workflow
  • ✅ ทำให้การตั้งค่า Native ง่ายขึ้น -> กำหนด permissions และ config ต่าง ๆ ผ่าน expo.config.js
  • ✅ Expo อัปเดต native dependencies ให้อัตโนมัติ -> ลดภาระการแก้ไขโค้ดเอง

🎯 สรุป

  • 📌 ไลบรารีที่ใช้ Native Modules อาจต้องใช้ Plugins
  • 📌 Expo Plugins ช่วยให้ใช้ Native Code ได้โดยไม่ต้อง Eject
  • 📌 บางไลบรารีต้องมีการตั้งค่า Permissions หรือ Native Config ผ่าน Plugins

ถ้าต้องการใช้ Expo แบบเต็มที่ ควรเช็ค Expo Plugins ก่อนติดตั้งไลบรารีใด ๆ 🚀

Loading