React 组件传 children 的各种方案

自定义组件的时候往往需要传 children,由于写法比较多样,我就总结了一下。

方案列表

  • [1. 类组件](#1. 类组件)
    • [1.1 类组件,不使用解构](#1.1 类组件,不使用解构)
    • [1.2 类组件,使用解构](#1.2 类组件,使用解构)
  • [2. 函数组件](#2. 函数组件)
    • [2.1 函数组件,不使用解构](#2.1 函数组件,不使用解构)
    • [2.2 函数组件,外部解构](#2.2 函数组件,外部解构)
    • [2.3 函数组件,内部解构](#2.3 函数组件,内部解构)
  • [3. 普通函数](#3. 普通函数)
    • [3.1 普通函数,内部解构](#3.1 普通函数,内部解构)
    • [3.2 普通函数,外部解构](#3.2 普通函数,外部解构)
    • [3.3 普通函数,外部解构,不使用自定义Type](#3.3 普通函数,外部解构,不使用自定义Type)
    • [3.4 普通函数,不使用解构,不使用自定义Type](#3.4 普通函数,不使用解构,不使用自定义Type)
  • 调用及展示

要自定义的组件是这样的:

其中包含一个 title 和一个 children

定义一个后面要用到的 Props:

typescript 复制代码
/** 定义属性对象
 * - title: 标题
 * - children: 子组件
 */
type Props = {
  title: string;
  children?: React.ReactNode;
};

1. 类组件

1.1 类组件,不使用解构

typescript 复制代码
class ClassComponent1 extends Component<Props> {
  render(): ReactNode {
    return (
      <div style={{ backgroundColor: 'red' }}>
        <h2>{this.props.title}</h2>
        {this.props.children}
      </div>
    );
  }
}

1.2 类组件,使用解构

typescript 复制代码
class ClassComponent2 extends Component<Props> {
  render(): ReactNode {
    // 解构赋值
    const { title, children } = this.props;
    return (
      <div style={{ backgroundColor: 'red' }}>
        <h2>{title}</h2>
        {children}
      </div>
    );
  }
}

2. 函数组件

2.1 函数组件,不使用解构

typescript 复制代码
const FunctionComponent1: React.FC<Props> = (props) => {
  return (
    <div style={{ backgroundColor: 'orange' }}>
      <h2>{props.title}</h2>
      {props.children}
    </div>
  );
};

2.2 函数组件,外部解构

typescript 复制代码
const FunctionComponent2: React.FC<Props> = ({ title, children }) => {
  return (
    <div style={{ backgroundColor: 'orange' }}>
      <h2>{title}</h2>
      {children}
    </div>
  );
};

2.3 函数组件,内部解构

typescript 复制代码
const FunctionComponent3: React.FC<Props> = (props) => {
  // 解构赋值
  const { title, children } = props;
  return (
    <div style={{ backgroundColor: 'orange' }}>
      <h2>{title}</h2>
      {children}
    </div>
  );
};

3. 普通函数

3.1 普通函数,内部解构

typescript 复制代码
function NormalFunction1(props: Props) {
  // 解构赋值
  const { title, children } = props;
  return (
    <div style={{ backgroundColor: 'yellow' }}>
      <h2>{title}</h2>
      {children}
    </div>
  );
}

3.2 普通函数,外部解构

typescript 复制代码
function NormalFunction2({ title, children }: Props) {
  return (
    <div style={{ backgroundColor: 'yellow' }}>
      <h2>{title}</h2>
      {children}
    </div>
  );
}

3.3 普通函数,外部解构,不使用自定义Type

typescript 复制代码
function NormalFunction3({
  title,
  children,
}: {
  title: string;
  children?: React.ReactNode;
}) {
  return (
    <div style={{ backgroundColor: 'yellow' }}>
      <h2>{title}</h2>
      {children}
    </div>
  );
}

3.4 普通函数,不使用解构,不使用自定义Type

typescript 复制代码
function NormalFunction4(props: { title: string; children?: React.ReactNode }) {
  return (
    <div style={{ backgroundColor: 'yellow' }}>
      <h2>{props.title}</h2>
      {props.children}
    </div>
  );
}

调用及展示

typescript 复制代码
export default class ChildrenPage extends Component {
  render() {
    return (
      <div style={{ padding: '20px' }}>
        <h1>组件传children</h1>
        <ClassComponent1 title="类组件,不使用解构">
          <p>这里是children</p>
        </ClassComponent1>
        <ClassComponent2 title="类组件,使用解构">
          <p>这里是children</p>
        </ClassComponent2>
        <FunctionComponent1 title="函数组件,不使用解构">
          <p>这是里children</p>
        </FunctionComponent1>
        <FunctionComponent2 title="函数组件,外部解构">
          <p>这是里children</p>
        </FunctionComponent2>
        <FunctionComponent3 title="函数组件,内部解构">
          <p>这是里children</p>
        </FunctionComponent3>
        <NormalFunction1 title="普通函数,内部解构">
          <p>这里是children</p>
        </NormalFunction1>
        <NormalFunction2 title="普通函数,外部解构">
          <p>这里是children</p>
        </NormalFunction2>
        <NormalFunction3 title="普通函数,外部解构,不使用自定义Type">
          <p>这里是children</p>
        </NormalFunction3>
        <NormalFunction4 title="普通函数,不使用解构,不使用自定义Type">
          <p>这里是children</p>
        </NormalFunction4>
      </div>
    );
  }
}
相关推荐
接着奏乐接着舞。4 分钟前
【2026年7月最新】69道RAG面试题
前端·人工智能·后端·aigc·embedding·rag
以和为贵4 分钟前
前端也能搞懂 Agent:从 Function Calling 到自主编排
前端·人工智能·架构
风止何安啊22 分钟前
🚦 前端并发请求 “交通管制”:别让你的接口堵成早高峰
前端·javascript·面试
J船长26 分钟前
扫盲烂笔头:A 记录、CNAME 的作用, Nginx的用途,为啥叫反向代理
前端
妙码生花32 分钟前
从 PHP 到 AI + Golang,程序员自救转型手记(二十六):icon 组件封装
前端·vue.js·typescript
李明卫杭州32 分钟前
Vue2 & Vue3 中 Router `props` 配置详解:彻底摆脱 `$route` 依赖
前端·vue.js·前端框架
掘金一周33 分钟前
裁员了,公司很爽快,赔偿一分没少 | 沸点周刊 7.8
前端·人工智能·后端
熊猫钓鱼>_>37 分钟前
2026 前端的三场底层革命:当 Vue、React Native 和 Three.js 同时去掉中间层
前端·javascript·vue.js·react native·架构·webgl·three.js
名字还没想好☜1 小时前
React useEffect 依赖数组踩坑:闭包陷阱与清理函数
前端·javascript·react.js·react·useeffect
宸翰2 小时前
uni-app 设置 Android 底部虚拟导航栏背景色
android·前端·uni-app