当前位置: 首页 > news >正文

React 组件传 children 的各种方案

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

方案列表

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


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

在这里插入图片描述

其中包含一个 title 和一个 children

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

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

1. 类组件

1.1 类组件,不使用解构

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

1.2 类组件,使用解构

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 函数组件,不使用解构

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

2.2 函数组件,外部解构

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

2.3 函数组件,内部解构

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

3. 普通函数

3.1 普通函数,内部解构

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

3.2 普通函数,外部解构

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

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

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

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

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

调用及展示

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>);}
}

在这里插入图片描述

http://www.lryc.cn/news/189102.html

相关文章:

  • 如何在一个传统的html中,引入vueJs并使用vue复制组件?
  • 【轻松玩转MacOS】故障排除篇
  • Linux基本指令(1)
  • 计算机毕业设计选题推荐-springboot 网上手机销售系统
  • 2、vscode c++ 项目配置调试及运行
  • 二叉搜索树的最近公共祖先
  • LuatOS-SOC接口文档(air780E)-- i2c - I2C操作
  • 帝国cms改目录后打不开,帝国cms改目录生成后还是404
  • 计算机毕业设计选什么题目好?springboot智慧养老中心管理系统
  • 创建一个基本的win32窗口
  • 如何在 Spring Boot 中使用 WebSocket
  • ubuntu2023装完显卡驱动和CUDA CUDNN开机只有下划线闪烁
  • MySQL三种安装方法(yum安装、编译安装、二进制安装)
  • 《视觉 SLAM 十四讲》第 7 讲 视觉里程计1 【如何根据图像 估计 相机运动】【特征点法】
  • 9. 一个SpringBoot项目运行
  • 如何实现chatGPT批量问答,不用token
  • Arduino驱动LIS2DH三轴加速度传感器(惯性测量传感器篇)
  • B 开组会(可持久线段树+树剖) 武汉大学2023年新生程序设计竞赛(同步赛)
  • vue的axios方法
  • gitlab docker部署,备份,恢复。附踩坑记录
  • 2023品牌新媒体矩阵营销洞察报告:流量内卷下,如何寻找增长新引擎?
  • HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Toggle
  • redis,mongoDB,mysql,Elasticsearch区别
  • 什么是软件测试架构师?
  • 安科瑞ARB5系列弧光保护装置,智能电弧光保护,保障用电安全
  • 查找算法——二分查找法
  • 大数据——Spark Streaming
  • graphviz 绘制二叉树
  • STM32 PA15/JTDI 用作普通IO,烧录口不能使用问题解决
  • 【ARM Coresight 系列文章 9 -- ETM 介绍 1】