Props
Passing 数据之间组件, prop destructuring, 默认值, and prop 类型.
你将学到
- 如何 通过 and 接收 Props 从 parent 到 child
- Destructuring Props for cleaner code
- 默认 prop 值 and children prop
- 为什么 Props 是读取-仅 and one-方式数据 flow
概念讲解
What Are Props?
Props (short for properties) are the mechanism for passing data from a parent component to a child component. They are the inputs to a component, much like function arguments. Props are always passed down the component tree — from parent to child — and they are read-only. A component must never modify its own props. This one-way data flow is a core React principle that keeps data flow predictable and debugging tractable.
Destructuring Props
Instead of accessing Props.name, Props.age, you can destructure props in the function signature for cleaner code: 函数 用户({ name, age }) {}. This is the idiomatic React style. You can also provide default values right in the signature: 函数 用户({ name = "Guest", age = 0 }) {}. Destructuring makes it immediately clear what inputs a component expects.
The children Prop
The special children prop contains whatever is passed between the opening and closing tags of a component. <Card>Hello</Card> passes "Hello" as children to Card. This is how you create wrapper and layout components. The children prop is powerful because it lets a component be agnostic about its content — the parent decides what goes inside.
Props Are Read-Only
A component must treat props as immutable. If you need to change a value, the parent should manage the state and pass a new prop value down. This is called lifting state up. Attempting to mutate props directly will not work and will cause confusing bugs. The unidirectional data flow (parent → child) is what makes React applications predictable and easy to reason about.
Prop Types and Validation
In modern React with TypeScript, you define prop types with interfaces, giving you compile-time type checking and autocomplete. Without TypeScript, you can use PropTypes for runtime validation. Always document what props a component expects, their types, and which are required. This makes your components self-documenting and catches integration errors early.
示例代码
// Destructuring props with default values for clean, readable code.
function UserCard({ name = "Guest", role = "Member", avatar, isActive }) {
return (
<div className={isActive ? "user-card active" : "user-card"}>
<img src={avatar} alt={name} className="avatar" />
<div className="user-info">
<h4>{name}</h4>
<span className="role">{role}</span>
<span className="status">{isActive ? "Online" : "Offline"}</span>
</div>
</div>
);
}
// Parent passes data down as props — one-way data flow.
function App() {
return (
<UserCard name="Alice" role="Admin" isActive={true}
avatar="/avatars/alice.png" />
);
}
Props 是 destructured in signature 使用 defaults. parent App 通过数据下. UserCard cannot 修改这些 Props — 它仅 读取 and 渲染它们.
动手试一试
- JSON Formatter
Props 是经常对象 — inspect and 格式它们清晰地.
- JSON 到 YAML
Convert prop-如 JSON 数据到 YAML for 配置 files.
常见错误
错误写法
Trying 到修改 Props directly 内部 child 组件
正确做法
Props 是读取-仅. 如果 child 需要到 更改数据, parent 应该 own 状态 and 通过回调 prop 下. child calls 回调, and parent 更新状态.
错误写法
Passing 也许多 individual Props 相反的 对象
正确做法
如果组件 takes 许多 Props, consider 分组 related ones 到对象. 但 avoid passing 整个 parent 状态 — 通过仅 什么 child 需要.
练习 Props
9 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。