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

การจัดการสถานะด้วยฮุก

เรียนรู้การจัดการข้อมูลเฉพาะคอมโพเนนต์โดยใช้ `useState` และฮุก React ที่จำเป็นอื่น ๆ

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

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

Intro to Component State

In React, state refers to data that a component manages internally and that can change over time, affecting how the component renders.

Think of it as a component's memory. When state changes, React efficiently updates only the necessary parts of your user interface.

Why State Matters

State is crucial for building interactive and dynamic web applications. Without it, your UI would be static and unable to respond to user actions.

  • User Input: Storing text from an input field.
  • Toggles: Managing whether a menu is open or closed.
  • Counters: Keeping track of a numerical value.

It makes your components 'alive'!

Meet the `useState` Hook

To add state to functional components in React, we use a special function called a Hook. The primary hook for managing local component state is useState.

Hooks are functions that let you 'hook into' React features like state and lifecycle methods from functional components.

Declaring State with `useState`

The useState hook returns an array with two items:

  1. The current state value.
  2. A function to update that state value.

We use array destructuring to get these values:

import React, { useState } from 'react';

function MyComponent() {
  // Declares a state variable 'count' initialized to 0
  // 'setCount' is the function to update 'count'
  const [count, setCount] = useState(0);

  // Declares a state variable 'name' initialized to an empty string
  const [name, setName] = useState('');

  return (
    <div>
      <p>Count: {count}</p>
      <p>Name: {name}</p>
    </div>
  );
}

Updating State: Simple Counter

To change the state, you must call the setter function (e.g., setCount). When you call this function, React will re-render your component with the new state value.

Try clicking the button to see the count change!

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount(count + 1); // Update the state
  };

  return (
    <div>
      <p>Current Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

State Updates are Asynchronous

It's important to know that state updates in React are often asynchronous. This means React might batch multiple state updates for performance.

Because of this, if your new state depends on the previous state, it's safer to pass a function to the setter.

Functional State Updates

When updating state based on its previous value, provide a function to the setter. This function receives the previous state as an argument.

This pattern ensures you're always working with the most up-to-date state, preventing subtle bugs.

import React, { useState } from 'react';

function SafeCounter() {
  const [count, setCount] = useState(0);

  const incrementByTwo = () => {
    // This ensures we always increment based on the latest count
    setCount(prevCount => prevCount + 1);
    setCount(prevCount => prevCount + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={incrementByTwo}>Add 2</button>
    </div>
  );
}

Managing User Input

useState is perfect for controlling form input elements. When an input's value is managed by React state, it's called a controlled component.

This gives you full control over the input's value and allows for easy validation or transformation.

Controlled Input Example

Here's how you can use useState to manage the value of a text input field. The onChange event handler updates the state every time the user types.

Type something into the box!

import React, { useState } from 'react';

function InputField() {
  const [text, setText] = useState('');

  const handleChange = (event) => {
    setText(event.target.value); // Update state with input's value
  };

  return (
    <div>
      <input type="text" value={text} onChange={handleChange} />
      <p>You typed: {text}</p>
    </div>
  );
}

Quick Check: `useState`

Which of the following statements about the useState hook in React are TRUE?

Recap: State with Hooks

You've now learned the core of state management in React using the useState hook!

  • What is State? Data that changes and affects component rendering.
  • useState: The primary hook to add state to functional components.
  • Syntax: const [value, setValue] = useState(initial);
  • Updating: Always use the setter function. For updates based on previous state, pass a function to the setter.
  • Use Cases: Counters, toggles, managing user input, and much more!

Mastering useState is a fundamental step in building interactive React apps.

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

บทเรียน “การจัดการสถานะด้วยฮุก” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการสถานะด้วยฮุก”

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

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

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

บทเรียน “การจัดการสถานะด้วยฮุก” ใช้เวลานานแค่ไหน

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

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

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

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

  1. คอมโพเนนต์ React และ JSX
  2. การจัดการสถานะด้วยฮุก
  3. พร็อปส์และการสื่อสารระหว่างคอมโพเนนต์
  4. การเรนเดอร์รายการ คีย์ และส่วนติดต่อผู้ใช้แบบมีเงื่อนไข
← กลับไปที่ Next.js 15 Fullstack Web Apps