คอมโพเนนต์ React และ JSX
ทำความเข้าใจส่วนประกอบพื้นฐานของแอปพลิเคชัน React และวิธีที่ JSX ช่วยให้การสร้างส่วนติดต่อผู้ใช้ง่ายขึ้น
คอมโพเนนต์ React และ JSX เป็นบทเรียน 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 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are React Components?
Welcome to the world of React! At its heart, React is all about components.
- Think of components as reusable, independent building blocks for your user interface (UI).
- They let you break down complex UIs into smaller, manageable pieces.
- Each component is like a JavaScript function that returns what should be displayed on the screen.
Meet JSX: JavaScript XML
Writing UI in pure JavaScript can get complicated. That's where JSX comes in!
- JSX is a syntax extension for JavaScript that lets you write HTML-like code directly within your JavaScript files.
- It makes defining your UI intuitive and familiar, blending HTML structure with JavaScript's power.
- Browsers don't understand JSX directly, so it gets transformed into regular JavaScript.
JSX: Behind the Scenes
While JSX looks like HTML, it's not. When your code is compiled (or 'transpiled'), JSX is converted into plain JavaScript function calls.
For example, <h1>Hello</h1> becomes something like React.createElement('h1', null, 'Hello').
This conversion is handled automatically by tools like Babel, allowing you to write cleaner, more readable UI code.
Your First Functional Component
In modern React, components are usually written as JavaScript functions. These are called Functional Components.
They take 'props' (short for properties) as input and return JSX, which describes what the UI should look like.
function WelcomeMessage() {
return (
<h1>Hello, CoddyKit!</h1>
);
}
// In a real app, this component would be rendered
// by ReactDOM or a framework like Next.js.
// For now, think of it as a blueprint for UI.JSX Rule 1: Single Root Element
A crucial rule in JSX is that every JSX expression must return a single root element.
This means you can't have two top-level elements directly next to each other. If you need to return multiple elements, wrap them in a single parent element, like a <div>, or use a Fragment (<></>) for a cleaner approach that doesn't add extra nodes to the DOM.
function UserCard() {
return (
<>
<h2>John Doe</h2>
<p>Software Developer</p>
</>
);
}
// Incorrect: return (
// <h2>John Doe</h2>
// <p>Software Developer</p>
// );JSX Rule 2: camelCase Props
While JSX looks like HTML, it's still JavaScript. This means some HTML attributes are renamed to follow JavaScript's camelCase convention.
classbecomesclassNameforbecomeshtmlFor- Event handlers like
onclickbecomeonClick
This helps avoid conflicts with JavaScript reserved keywords.
<div className="container">
<label htmlFor="username">Name:</label>
<input type="text" id="username" />
</div>JavaScript Expressions in Action
One of the most powerful features of JSX is the ability to embed JavaScript expressions directly within your UI using curly braces {}.
You can use variables, perform calculations, or call functions inside these braces. Let's see a simple runnable example of a variable:
function main() {
const appName = "CoddyKit";
console.log(`Welcome to ${appName}!`);
}
main();Self-Closing Elements
In HTML, some tags can be self-closing (like <img> or <input>). In JSX, all elements must either have a closing tag or be self-closing.
If an element has no children, you must close it with a slash before the closing angle bracket, like <img /> or <br />.
<div>
<input type="text" placeholder="Enter your name" />
<br />
<img src="/logo.png" alt="App Logo" />
</div>Composing Components
The real magic of React components is their ability to be composed. You can use one component inside another, building complex UIs from simple, focused pieces.
This promotes reusability and makes your application's structure clear and maintainable. It's like building with LEGO bricks!
function Button() {
return <button>Click Me</button>;
}
function Header() {
return (
<header>
<h1>My Awesome App</h1>
<Button /> {/* Using the Button component */}
</header>
);
}Component & JSX Check
Time for a quick check! Which of the following is a key rule when writing JSX?
Recap: Components & JSX
Great job! In this lesson, you've learned the foundational concepts of React:
- Components are the reusable building blocks of your UI, typically written as functions.
- JSX is a syntax extension for JavaScript that lets you write HTML-like code for your UI.
- Key JSX rules include returning a single root element, using camelCase for attributes (like
className), and self-closing empty tags. - You can embed JavaScript expressions within JSX using curly braces
{}.
Understanding components and JSX is crucial for building any React application, including those with Next.js!
คำถามที่พบบ่อย
บทเรียน “คอมโพเนนต์ React และ JSX” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “คอมโพเนนต์ React และ JSX” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack Web Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “คอมโพเนนต์ React และ JSX”
ทำความเข้าใจส่วนประกอบพื้นฐานของแอปพลิเคชัน React และวิธีที่ JSX ช่วยให้การสร้างส่วนติดต่อผู้ใช้ง่ายขึ้น คุณปฏิบัติ Next.js 15 Fullstack Web Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack Web Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack Web Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “คอมโพเนนต์ React และ JSX” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack Web Apps นี้ได้ไหม
ได้ บทเรียน Next.js 15 Fullstack Web Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- คอมโพเนนต์ React และ JSX
- การจัดการสถานะด้วยฮุก
- พร็อปส์และการสื่อสารระหว่างคอมโพเนนต์
- การเรนเดอร์รายการ คีย์ และส่วนติดต่อผู้ใช้แบบมีเงื่อนไข