React Native Academy · บทเรียน

การเรียกใช้บนอุปกรณ์และเครื่องจำลอง

เปิดใช้แอปบนอุปกรณ์จริงด้วย Expo Go และบนเครื่องจำลอง จากนั้นเรียนรู้วิธีโหลดใหม่และตรวจสอบการเปลี่ยนแปลงแบบเรียลไทม์

บทเรียน 3 จาก 413 ขั้นตอน

การเรียกใช้บนอุปกรณ์และเครื่องจำลอง เป็นบทเรียน React Native Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน React Native Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Starting the Development Server

To run your app, go to its folder and run npx expo start. That launches Metro and shows a QR code. Keep your phone and computer on the same Wi-Fi for it to connect.

# Start the Expo development server
cd MyFirstApp
npx expo start

# Force iOS Simulator directly
npx expo start --ios

# Force Android Emulator directly
npx expo start --android

# Start in web browser mode
npx expo start --web

Running on iOS Simulator

With Metro running, press i to launch the iOS Simulator. The first load installs Expo Go and takes a moment; after that it's fast. macOS only, sorry. 🍎

# With Metro running, press 'i' in the terminal
# Or launch a specific simulator:
xcrun simctl boot 'iPhone 15 Pro'

# List available devices
xcrun simctl list devices

# Open the Simulator app
open -a Simulator

Running on Android Emulator

Press a to open your app on the Android Emulator. Create an AVD in Android Studio first (or let Expo boot one). Android runs on any OS — handy if you don't have a Mac.

# List available Android Virtual Devices
emulator -list-avds

# Start a specific AVD
emulator -avd Pixel_7_API_34

# Verify the device is visible to adb
adb devices
# Output: List of devices attached
# emulator-5554  device

# Then press 'a' in Metro terminal

Scanning QR Code with a Real Device

To run on a real phone, install Expo Go and scan the QR code — Camera on iOS, the Expo Go app on Android. Real devices matter: simulators can't fully fake sensors or touch.

# Ensure Metro is running
npx expo start

# If QR scan doesn't work (firewall/VPN), use tunnel mode:
npx expo start --tunnel
# This routes traffic through Expo's servers,
# useful when local network is blocked.

# Or connect via USB and use:
npx expo start --localhost

Fast Refresh: Instant Code Updates

Fast Refresh pushes your edits to the running app in under a second and keeps your state — a counter at 5 stays at 5. It only resets when you change structure or hit an error.

// When you save this file, the app updates instantly
import { useState } from 'react';
import { Text, View, TouchableOpacity } from 'react-native';

export default function Counter() {
  const [count, setCount] = useState(0);
  return (
    <View>
      {/* Change 'blue' to 'red' and save — count stays at its current value */}
      <Text style={{ color: 'blue', fontSize: 40 }}>{count}</Text>
      <TouchableOpacity onPress={() => setCount(c => c + 1)}>
        <Text>Increment</Text>
      </TouchableOpacity>
    </View>
  );
}

The Developer Menu

Shake your device or press m to open the Developer Menu. From there you can reload, toggle Fast Refresh, open React DevTools, or watch FPS and memory live.

// You can programmatically open the dev menu
// (useful for testing)
import { DevMenu } from 'expo-dev-client';

// In a dev build:
DevMenu.openMenu();

// Keyboard shortcuts in Metro terminal:
// r → Reload app
// m → Open developer menu
// j → Open React DevTools
// i → Open iOS Simulator
// a → Open Android Emulator

USB Debugging on Android

Connect an Android phone over USB for a faster, firewall-proof link than Wi-Fi. Enable Developer Options and USB Debugging, run adb devices to confirm, then press a in Metro.

# 1. Enable Developer Options on Android:
# Settings → About Phone → Build number (tap 7 times)
# Settings → Developer Options → USB Debugging ON

# 2. Connect phone via USB and verify:
adb devices
# List of devices attached
# R5CT109XXXXX  device

# 3. Forward Metro port to the device
adb reverse tcp:8081 tcp:8081

# 4. Press 'a' in Metro to build and install

Reloading vs. Fast Refresh

Two ways to update your app: Fast Refresh patches one module and keeps state, while a full reload (press r) restarts everything. Seeing a stale UI? Try a full reload first.

// Scenarios where full reload is needed:
// 1. You changed a global constant
// 2. You updated app.json
// 3. You added/removed a Context Provider
// 4. You installed a new native module

// Force reload programmatically (dev only)
import { DevSettings } from 'react-native';

function DebugReloadButton() {
  if (!__DEV__) return null;
  return (
    <TouchableOpacity onPress={() => DevSettings.reload()}>
      <Text>Reload App</Text>
    </TouchableOpacity>
  );
}

Tunnel Mode for Remote Development

On a different network, like a VPN or locked-down Wi-Fi? Use tunnel mode with --tunnel. Expo routes through its servers so your app works anywhere — just a bit slower.

# Start in tunnel mode (works across networks)
npx expo start --tunnel

# Expo will print a URL like:
# https://u.expo.dev/your-project-id?channel-name=...

# Or switch modes interactively in Metro:
# Press 's' to switch between:
#   LAN (local Wi-Fi, fastest)
#   Tunnel (public URL, works anywhere)
#   localhost (USB/same machine only)

Expo Development Builds

Need native modules Expo Go lacks? Make a development build with EAS — a custom client with your native dependencies. Then expo start --dev-client connects to it.

# Create a development build with EAS
npx eas build --profile development --platform ios
npx eas build --profile development --platform android

# After installing the dev build on device:
npx expo start --dev-client

# eas.json configuration for dev build:
{
  'build': {
    'development': {
      'developmentClient': true,
      'distribution': 'internal'
    }
  }
}

Reading Console Logs from Device

Your console.log messages show up in the Metro terminal. For deeper digging, shake the device to open the JS debugger, or use React DevTools to inspect the component tree.

// console.log appears in Metro terminal
console.log('Hello from React Native!');
console.warn('This shows a yellow warning box');
console.error('This shows a red error box');

// Structured logging for objects
const user = { name: 'Alice', age: 30 };
console.log('User data:', JSON.stringify(user, null, 2));

// Use console.table for arrays
console.table([{ id: 1, name: 'Item A' }, { id: 2, name: 'Item B' }]);

Quick Check

Test your understanding of React Native Mobile Development concepts from this lesson.

Lesson Recap

You learned to run your app: expo start launches Metro and a QR code, Fast Refresh updates the UI live, and tunnel mode works across networks. Next: project structure! 🗂️

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “การเรียกใช้บนอุปกรณ์และเครื่องจำลอง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเรียกใช้บนอุปกรณ์และเครื่องจำลอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส React Native Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเรียกใช้บนอุปกรณ์และเครื่องจำลอง”

เปิดใช้แอปบนอุปกรณ์จริงด้วย Expo Go และบนเครื่องจำลอง จากนั้นเรียนรู้วิธีโหลดใหม่และตรวจสอบการเปลี่ยนแปลงแบบเรียลไทม์ คุณปฏิบัติ React Native Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน React Native Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน React Native Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเรียกใช้บนอุปกรณ์และเครื่องจำลอง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน React Native Academy นี้ได้ไหม

ได้ บทเรียน React Native Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การติดตั้ง Node, Expo CLI และเครื่องจำลอง
  2. การสร้างโครงการ Expo แรกของคุณ
  3. การเรียกใช้บนอุปกรณ์และเครื่องจำลอง
  4. ทำความเข้าใจโครงสร้างโครงการ
← กลับไปที่ React Native Academy