Skip to content

Props

一句话解释

Props (属性) 是读取-仅输入通过从 parent 组件到 child 组件到 配置它的 behavior or 内容.

用大白话说

Props 是 mechanism for passing 数据下 组件树. parent 组件通过 Props 到 child 通过编写它们 as attributes in JSX, 只是如 HTML attributes: <Avatar user={user} size="大" />.

Inside the child, props arrive as a plain JavaScript object. They are read-only — a component must never modify its own props. If data needs to change, the parent should pass new props, or the child should use state.

Props flow in one direction: top-down (parent → child). This unidirectional data flow makes React apps predictable and easier to debug. To send data back up, you pass callback functions as props.

示例

jsx
function Welcome({ name, age }) {
  return <p>{name} is {age} years old.</p>;
}

// Parent passes props
<Welcome name="Alice" age={30} />

parent 通过 name and age as Props. child destructures 它们从 Props 对象 and 渲染它们.

常见混淆

  • 容易混淆的对象: 状态

    关键区别: Props 是通过从 parent and 是读取-仅. 状态是 内部到 组件 and 能是 更改使用 setState. Props 是如 函数参数; 状态是 如局部变量.

相关术语

相关资源

相关课程

  • Props

    学习如何 Props 工作 and 如何 通过数据之间组件.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表