React 组件与 JSX
理解 React 应用程序的构成要素,以及 JSX 如何简化用户界面的创建。
React 组件与 JSX 是 CoddyKit 上的免费 Next.js 15 Fullstack Web Apps 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。
学习 Next.js 15 Fullstack Web Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack Web Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「React 组件与 JSX」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack Web Apps 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack Web Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- React 组件与 JSX
- 使用 Hooks 管理状态
- 属性与组件通信
- 渲染列表、键与条件式 UI