在 React 中,Props(属性)是由父组件传递给子组件的数据。Props 是组件之间传递信息的方式。与状态(State)不同,Props 是不可变的,子组件不能修改它们。Props 是读取组件外部数据的唯一途径,是 React 组件的核心组成部分之一。

目录

  1. 什么是 Props
  2. 如何使用 Props
  3. 默认 Props
  4. Prop 类型验证
  5. 子组件传递数据给父组件
  6. 解构 Props
  7. Props 的最佳实践
  8. 参考资料

1. 什么是 Props

Props 是从父组件传递到子组件的配置项,包含了子组件的动态内容。每当父组件更新时,子组件也会接收到新的 props 并重新渲染。

  • 只读:Props 是只读的,子组件不能修改 Props。
  • 不可变:通过 Props 传递的数据应该被视为不可变的。如果需要修改数据,应该通过状态(State)来实现。
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

在上面的例子中,name 是通过 props 传递给 Greeting 组件的。


2. 如何使用 Props

Props 是通过组件的参数传递的,可以通过 props 对象在子组件中访问传递的数据。

2.1 基本用法

父组件将数据通过属性传递给子组件。

// 父组件
function App() {
  return <Greeting name="Alice" />;
}

// 子组件
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

在上面的代码中,App 组件通过 props 将 name="Alice" 传递给 Greeting 组件,子组件 Greeting 接收到 props.name 后渲染内容。

2.2 使用 JSX 属性传递 Props

你还可以在 JSX 中直接传递数据作为 Props:

function App() {
  const person = { name: 'Alice', age: 30 };
  return <Person {...person} />;
}

function Person(props) {
  return (
    <div>
      <h1>{props.name}</h1>
      <p>{props.age}</p>
    </div>
  );
}

在这个例子中,Person 组件接收并渲染对象 person 中的属性。


3. 默认 Props

组件的 Props 可以设置默认值,当父组件没有传递某个属性时,组件会使用默认值。可以通过 defaultProps 属性来设置默认值。

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

Greeting.defaultProps = {
  name: 'Stranger', // 设置默认值
};

function App() {
  return <Greeting />;
}

如果 Greeting 组件没有接收到 name 属性,它将使用 defaultProps 中的默认值 Stranger


4. Prop 类型验证

React 提供了 prop-types 库,用于对组件的 props 进行类型验证。通过类型验证,可以确保组件的 props 类型正确,从而减少错误。

4.1 安装 PropTypes

npm install prop-types

4.2 使用 PropTypes 进行验证

import PropTypes from 'prop-types';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

Greeting.propTypes = {
  name: PropTypes.string.isRequired, // name 属性必须是字符串,且是必需的
};

function App() {
  return <Greeting name="Alice" />;
}

在上面的代码中,Greeting 组件要求 name 必须是字符串类型,并且是必填项。如果没有传递 name 或类型不匹配,React 会给出警告。


5. 子组件传递数据给父组件

通常情况下,父组件通过 props 向子组件传递数据,但有时子组件需要向父组件传递数据。这通常是通过父组件传递给子组件的回调函数来实现的。

5.1 回调函数传递数据

父组件定义一个方法,并通过 props 传递给子组件,子组件调用这个方法并传递数据回父组件。

function Parent() {
  const handleMessage = (message) => {
    alert('Message from child: ' + message);
  };

  return <Child onMessage={handleMessage} />;
}

function Child(props) {
  return (
    <button onClick={() => props.onMessage('Hello from child!')}>
      Send Message
    </button>
  );
}

在这个例子中,Parent 组件向 Child 组件传递了一个回调函数 handleMessage,当子组件点击按钮时,调用 onMessage 回调并向父组件传递数据。


6. 解构 Props

为了简化对 props 的访问,可以直接在函数参数中解构 Props。

6.1 解构 Props

function Greeting({ name, age }) {
  return (
    <div>
      <h1>Hello, {name}!</h1>
      <p>Age: {age}</p>
    </div>
  );
}

function App() {
  return <Greeting name="Alice" age={30} />;
}

使用解构后,Greeting 组件直接在参数中提取了 nameage,使代码更加简洁。


7. Props 的最佳实践

  • 避免不必要的 Props:只传递组件实际需要的 Props,避免传递不必要的数据。
  • 保持 Props 只读:不要在子组件中修改 props,Props 是不可变的。
  • Prop 类型验证:使用 prop-types 或 TypeScript 进行 Props 类型验证,确保组件收到正确的数据类型。
  • 解构 Props:解构 Props 可以提高代码的可读性和简洁性。
  • 默认 Props:使用 defaultProps 来设置 Props 的默认值,确保组件在缺少某些 Props 时能够正常工作。

8. 参考资料

出站链接

站内链接


通过合理使用 Props,你可以在 React 中实现父子组件的数据传递,并构建动态和交互式的用户界面。