组件
一句话解释
组件是 reusable, self-包含片段的 UI 编写 as JavaScript 函数那个返回 JSX.
用大白话说
component 是the fundamental building block of a React application。 Think of it as a custom HTML element you define yourself — it encapsulates some markup, some styling, and some behavior, and you can reuse it anywhere.
A function component is simply a JavaScript function that receives a Props object and returns JSX describing what should appear on screen. React calls your function whenever the props or state change, and updates the DOM to match the returned JSX.
Components let you break a complex UI into small, understandable pieces. A page might be composed of a <请求头 />, a <Sidebar />, a <ArticleList />, and a <Footer />. Each is independent, testable, and reusable.
示例
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
// Reuse the component with different data
<Greeting name="Alice" />
<Greeting name="Bob" />Greeting 是函数组件. 它接收 name 通过 Props and 返回 JSX. 你能 渲染它 许多次数使用不同数据.
常见混淆
容易混淆的对象: 元素
关键区别: 组件是 函数/类定义 (blueprint). 元素是 plain 对象描述什么到 渲染 — 创建通过 invoking 组件如
<Greeting />orReact.createElement(Greeting).
相关术语
相关资源
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。