React Native Academy · บทเรียน

การนำทางระหว่างหน้าจอและการส่งพารามิเตอร์

ใช้ navigation.navigate เพื่อย้ายระหว่างหน้าจอ ส่งพารามิเตอร์ในอาร์กิวเมนต์ที่สอง และอ่านค่าด้วย route.params บนหน้าจอปลายทาง

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

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

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

The navigation Prop in Screens

Every component registered as a screen in a React Navigation navigator automatically receives a navigation prop. This object provides methods to move between screens, go back, and manage the navigation stack. You can also access it in nested components using the useNavigation hook.

function HomeScreen({ navigation }) {
  // navigation.navigate, navigation.goBack, etc.
  return <View />;
}

Navigating with navigation.navigate

Call navigation.navigate('ScreenName') to move to another screen. If the screen is already in the stack and you navigate to it again, React Navigation will go to the existing instance rather than adding a duplicate. Use navigation.push('ScreenName') to always push a new instance.

function HomeScreen({ navigation }) {
  return (
    <Button
      title='Go to Details'
      onPress={() => navigation.navigate('Details')}
    />
  );
}

Going Back with navigation.goBack

Call navigation.goBack() to pop the current screen off the stack and return to the previous one. On iOS the swipe-back gesture is enabled by default. On Android, the hardware back button calls goBack automatically. Use navigation.popToTop() to jump all the way back to the first screen.

function DetailsScreen({ navigation }) {
  return (
    <Button
      title='Go Back'
      onPress={() => navigation.goBack()}
    />
  );
}

Passing Params When Navigating

Pass a second argument to navigation.navigate to send parameters to the destination screen. The params object can contain any serializable data — IDs, strings, booleans. Avoid passing complex non-serializable objects like class instances or functions.

function HomeScreen({ navigation }) {
  return (
    <Button
      title='View Post'
      onPress={() =>
        navigation.navigate('Details', {
          postId: 42,
          title: 'React Navigation Deep Dive',
        })
      }
    />
  );
}

Reading Params with route.params

The destination screen receives a route prop alongside the navigation prop. Access the parameters you passed via route.params. Always provide defaults in case a screen is opened without params, for example when it is the initial screen.

function DetailsScreen({ route, navigation }) {
  const { postId, title } = route.params ?? {};
  return (
    <View>
      <Text>Post ID: {postId}</Text>
      <Text>Title: {title}</Text>
    </View>
  );
}

Default Params on Stack.Screen

Set initialParams on a Stack.Screen to provide default parameter values. These defaults merge with any params passed at runtime, so the screen always has a complete set of values to work with, even when opened as the initial route.

<Stack.Screen
  name='Details'
  component={DetailsScreen}
  initialParams={{ postId: 0, title: 'Unknown' }}
/>

Updating Params from a Screen

A screen can update its own params using navigation.setParams. This is useful when an action on the current screen (like editing a form field) should be reflected in the navigation header title. The params update triggers a re-render of the screen.

function DetailsScreen({ route, navigation }) {
  const { title } = route.params;

  return (
    <Button
      title='Update Title'
      onPress={() => navigation.setParams({ title: 'Updated Title' })}
    />
  );
}

Using useNavigation Hook

Deep child components that are not registered screens do not receive the navigation prop automatically. Use the useNavigation() hook from React Navigation to access the navigation object anywhere inside the navigator tree without prop drilling.

import { useNavigation } from '@react-navigation/native';

function MyButton() {
  const navigation = useNavigation();
  return (
    <Button
      title='Go to Profile'
      onPress={() => navigation.navigate('Profile')}
    />
  );
}

Using useRoute Hook

Similarly, use the useRoute() hook to access route.params in a non-screen component. This is useful when you have a deeply nested component that needs to read the current screen's parameters without threading the route prop through every level.

import { useRoute } from '@react-navigation/native';

function PostHeader() {
  const route = useRoute();
  const { title } = route.params ?? {};
  return <Text style={{ fontWeight: 'bold' }}>{title}</Text>;
}

Setting the Header Title from Params

You can dynamically set the navigation header title based on route params by passing a function to the options prop. The function receives the route object, allowing you to use param values as the screen title without extra state.

<Stack.Screen
  name='Details'
  component={DetailsScreen}
  options={({ route }) => ({ title: route.params.title ?? 'Details' })}
/>

Navigating Back and Sending Data Back

To send data from a child screen back to its parent, use navigation.navigate with the parent screen's name and include the return data as params. The parent screen will receive the updated params. Alternatively, pass a callback function as a param to the child screen and call it before going back.

// In child screen
navigation.navigate('Home', { selectedItem: 'apple' });

// In parent screen — read route.params.selectedItem

Quick Check

Test your understanding of React Navigation screen navigation and params from this lesson.

Lesson Recap

In this lesson you learned: use navigation.navigate to move to another screen, pass params as the second argument to navigate, and read params with route.params in the destination screen. Next up we explore the Bottom Tab Navigator.

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

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

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

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

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

บทเรียน “การนำทางระหว่างหน้าจอและการส่งพารามิเตอร์” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การนำทางระหว่างหน้าจอและการส่งพารามิเตอร์”

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

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

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

บทเรียน “การนำทางระหว่างหน้าจอและการส่งพารามิเตอร์” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การติดตั้งและกำหนดค่า React Navigation
  2. การนำทางระหว่างหน้าจอและการส่งพารามิเตอร์
  3. ตัวนำทางแท็บด้านล่าง
  4. ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน
← กลับไปที่ React Native Academy