0Pricing
Next.js 15 Fullstack Web Apps · บทเรียน

คอมโพเนนต์ควบคุมและสถานะ

จัดการค่าข้อมูลนำเข้าของแบบฟอร์มโดยใช้รูปแบบคอมโพเนนต์ควบคุมของ React เพื่อให้พฤติกรรมคาดเดาได้

คอมโพเนนต์ควบคุมและสถานะ เป็นบทเรียน Next.js 15 Fullstack Web Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Next.js 15 Fullstack Web Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน

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

Forms: User Interaction Hub

Forms are essential for almost any web application! They allow users to input information, make choices, and interact with your app.

Think about login screens, search bars, feedback forms, or even shopping cart quantity selectors – all rely on forms.

Uncontrolled Inputs: A Quick Look

In plain HTML, input elements manage their own state. When you type into an <input>, its value updates internally.

In React, these are called uncontrolled components. While simple for static forms, they can be tricky to integrate with React's dynamic UI updates and state management.

Meet Controlled Components

Controlled components are input elements whose values are controlled by React state. Instead of the DOM managing the input's value, your React component's state becomes the 'single source of truth'.

This gives you full control over the form data, making it predictable and easier to manage.

The Core: `value` & `onChange`

Two props are key to controlled components:

  • value: Sets the current displayed value of the input. It should always come from your component's state.
  • onChange: A function that gets called whenever the input's value changes (e.g., user types). This function updates your component's state.

Controlled Text Input in Action

Let's see a basic controlled text input. We use useState to hold the input's value and update it with onChange.

Try typing in the input below:

import React, { useState } from 'react';

function App() {
  const [name, setName] = useState('');

  const handleChange = (event) => {
    setName(event.target.value);
  };

  return (
    <div>
      <label>Your Name:</label>
      <input
        type="text"
        value={name}
        onChange={handleChange}
      />
      <p>Hello, {name || 'stranger'}!</p>
    </div>
  );
}

State is the Single Source of Truth

Here's the flow for a controlled input:

  1. The input's value prop is set by React state (e.g., name).
  2. User types a character.
  3. The onChange event fires, calling handleChange.
  4. handleChange updates the state (setName).
  5. React re-renders the component.
  6. The input's value prop is now updated with the new state, displaying the typed character.

Other Controlled Elements

The same value and onChange pattern applies to other form elements like <textarea> and <select>.

For <select>, the value prop is set on the <select> tag itself, not on individual <option> tags.

import React, { useState } from 'react';

function App() {
  const [feedback, setFeedback] = useState('');
  const [fruit, setFruit] = useState('apple');

  return (
    <div>
      <label>Your Feedback:</label>
      <textarea
        value={feedback}
        onChange={(e) => setFeedback(e.target.value)}
        rows="3"
      />
      <p>Feedback: {feedback}</p>

      <label>Favorite Fruit:</label>
      <select
        value={fruit}
        onChange={(e) => setFruit(e.target.value)}
      >
        <option value="apple">Apple</option>
        <option value="banana">Banana</option>
        <option value="orange">Orange</option>
      </select>
      <p>Selected: {fruit}</p>
    </div>
  );
}

Why Control Matters

Controlling your form inputs provides many benefits:

  • Instant Validation: Validate input as the user types.
  • Conditional Logic: Enable/disable buttons based on input state.
  • Formatted Input: Automatically format phone numbers or currencies.
  • Easy State Access: Always know the current value of any input.
  • Predictable Behavior: Your UI always reflects your application's state.

Managing Many Inputs

For forms with multiple inputs, you can use a single state object and a generic onChange handler. The name attribute of the input helps identify which state property to update.

import React, { useState } from 'react';

function App() {
  const [formData, setFormData] = useState({
    username: '',
    email: ''
  });

  const handleChange = (event) => {
    const { name, value } = event.target;
    setFormData(prevData => ({ ...prevData, [name]: value }));
  };

  return (
    <form>
      <label>Username:</label>
      <input
        type="text"
        name="username"
        value={formData.username}
        onChange={handleChange}
      />
      <label>Email:</label>
      <input
        type="email"
        name="email"
        value={formData.email}
        onChange={handleChange}
      />
      <p>User: {formData.username}, Email: {formData.email}</p>
    </form>
  );
}

Controlled Input Challenge

Which of the following are key characteristics of a controlled component in React?

Recap: Controlled & Predictable

You've learned about controlled components!

  • They link input values directly to React state.
  • They use the value prop for display and an onChange handler to update state.
  • This pattern provides full control, enabling features like instant validation and predictable behavior.
  • It applies to <input>, <textarea>, and <select> elements.

Mastering controlled components is a crucial step for building robust forms in Next.js applications!

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

บทเรียน “คอมโพเนนต์ควบคุมและสถานะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “คอมโพเนนต์ควบคุมและสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack Web Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “คอมโพเนนต์ควบคุมและสถานะ”

จัดการค่าข้อมูลนำเข้าของแบบฟอร์มโดยใช้รูปแบบคอมโพเนนต์ควบคุมของ React เพื่อให้พฤติกรรมคาดเดาได้ คุณปฏิบัติ Next.js 15 Fullstack Web Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack Web Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack Web Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “คอมโพเนนต์ควบคุมและสถานะ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack Web Apps นี้ได้ไหม

ได้ บทเรียน Next.js 15 Fullstack Web Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. คอมโพเนนต์ควบคุมและสถานะ
  2. การตรวจสอบแบบฟอร์มด้วย React Hook Form
  3. แบบฟอร์มฟูลสแตกด้วยการดำเนินการเซิร์ฟเวอร์
  4. การอัปโหลดไฟล์และการจัดการฟอร์มแบบหลายส่วน
← กลับไปที่ Next.js 15 Fullstack Web Apps