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>
    );
  }
}
相关推荐
工程师老罗1 小时前
Image(图像)的用法
java·前端·javascript
早點睡3901 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-swiper
javascript·react native·react.js
swipe2 小时前
把 JavaScript 原型讲透:从 `[[Prototype]]`、`prototype` 到 `constructor` 的完整心智模型
前端·javascript·面试
问道飞鱼2 小时前
【前端知识】React 组件生命周期:从底层原理到实践场景
前端·react.js·前端框架·生命周期
CHU7290352 小时前
定制专属美丽时刻:美容预约商城小程序的贴心设计
前端·小程序
浩~~3 小时前
反射型XSS注入
前端·xss
AwesomeDevin3 小时前
AI时代,我们的任务不应沉溺于与 AI 聊天,🤔 从“对话式编程”迈向“数字软件工厂”
前端·后端·架构
harrain3 小时前
antvG2折线图和区间range标记同时绘制
前端·javascript·vue.js·antv·g2
德育处主任Pro3 小时前
从重复搭建到高效生产,RollCode的H5开发新范式
前端
蜡台4 小时前
SPA(Single Page Application) Web 应用(即单页应用)架构模式 更新
前端·架构·vue·react·spa·spa更新