组件
函数组件, composition, and 构建块 的每个 React application.
你将学到
- 如何 定义 and use 函数组件
- Composing 小组件到 larger UIs
- difference 之间函数 and 类组件
- Naming 约定 and file 组织 for 组件
概念讲解
What Is a Component?
A React component is a JavaScript function that returns JSX. It takes optional inputs called props and returns a description of what should appear on the screen. Components are the atomic unit of a React application — everything you see is built from components. A button is a component, a form is a component, a whole page is a component.
Composition Over Inheritance
React strongly favors composition over inheritance. Instead of creating deep class hierarchies, you build complex UIs by combining simple, focused components. A Card component might contain a 请求头, a 请求体, and a Footer. Each is independent, reusable, and testable. This approach scales far better than inheritance and keeps each component small and understandable.
Function vs Class Components
Historically, React supported two types of components: class components and function components. Today, function components are the standard. Class components are still supported for backwards compatibility but are rarely written in new code. Function components are simpler, have less boilerplate, and work seamlessly with Hooks (which you will learn about soon). If you see old tutorials using 类 MyComponent extends React.组件, know that this is legacy syntax.
Naming and Organization
Component names must start with a capital letter — React treats lowercase tags as DOM elements (<div>) and capitalized tags as components (<MyCard>). Organize components into files: one primary component per file, named the same as the file. Group related components into folders. Keep components small — if a component exceeds 200 lines, consider splitting it.
Component Reusability
The power of components lies in reusability. A well-designed component is generic enough to be used in multiple contexts but specific enough to be meaningful. Avoid hard-coding values that should be props. Use sensible default props so the component works out of the box but can be customized. The children prop is especially powerful for creating wrapper components that do not care about their content — like layout containers, cards, and panels.
示例代码
// Small, focused, reusable components composed together.
function Button({ label, onClick }) {
return <button className="btn" onClick={onClick}>{label}</button>;
}
function Card({ title, children }) {
return (
<div className="card">
<h3 className="card-title">{title}</h3>
<div className="card-body">{children}</div>
</div>
);
}
// Compose them into a complete UI.
function App() {
return (
<Card title="Welcome">
<p>This is the card content.</p>
<Button label="Click me" onClick={() => alert("Clicked!")} />
</Card>
);
}
Button and Card 是 independent, reusable 组件. children prop lets Card 接受任何内容. App composes 它们 together — 这个是 composition in 操作.
动手试一试
- JSON Formatter
组件 Props 是经常 JSON 对象 — 格式 and inspect 它们.
- Code Diff
Compare 组件 versions 当 refactoring.
常见错误
错误写法
开始组件 names 使用 lowercase letter
正确做法
React treats lowercase 标签 as DOM 元素 (如 <div>) and capitalized 标签 as 组件. 总是 capitalize 组件 names: 函数 MyCard(), 不函数 myCard().
错误写法
制作组件也 大使用也 许多 responsibilities
正确做法
Keep 组件小 and focused. 如果组件做 也 much, 拆分它 到 smaller sub-组件 and compose 它们. Aim for one responsibility 每组件.
练习 组件
7 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。