JS 框架 词条目录
前端Wiki/词条/props

props

定义组件的外部输入接口,父组件通过 props 向子组件传递数据

props 是组件的「函数参数」:父组件通过属性把数据传进去,子组件只读使用——直接修改 props 是反模式(React 报错,Vue 报警告)。需要改,就让子组件 emit 事件、父组件改状态,数据流保持单向清晰。

props 支持默认值、类型校验、必填检查。Vue 用 defineProps 声明,React 用函数参数接收,两者都遵循「外部数据只读」这一铁律。

JavaScript
// Vue:声明式 props
const props = defineProps({
  title: { type: String, required: true },
  count: { type: Number, default: 0 }
});

// React:函数参数
function Card({ title, count = 0 }) {
  return <h2>{title}({count})</h2>;
}
⚠ 常见误区
🔗 相关词条state组件通信虚拟 DOM响应式原理
← 返回来源章节js-frameworks/g1c1s1.html