react 组件封装原则_react antd组件封装

这篇博客介绍了如何封装和使用Ant Design的Button和Tabs组件。首先展示了DsButton组件的实现,它接收children属性并返回。然后详细讲解了DsTabs组件的创建,包括遍历children属性以构建TabPane,并提供了回调函数。DsTabs组件可以接受嵌套的Panel组件,并展示其内容。
摘要由CSDN通过智能技术生成

简单组件

import React from "react";

import { Button } from "antd";

const DsButton = ({ children, ...res }) => {children};

export default DsButton;

使用

import DsButton from "./components/Button";

Button111

嵌套组件Tabs

import { Tabs } from 'antd';

const { TabPane } = Tabs;

function callback(key) {

console.log(key);

}

const Demo = () => (

Content of Tab Pane 1

Content of Tab Pane 2

);

ReactDOM.render(, mountNode);

import React from "react";

import { Tabs } from "antd";

const { TabPane } = Tabs;

const DsTabs = (props) => {

const { children, ...res } = props;

console.log("children", props, children);

return (

{children.map((v, k) => {

const { children, title, ...res } = v.props;

if (v.type === "panel")

return (

//必须有key, callback(key)

{children}

);

})}

);

};

export default DsTabs;

使用

import DsTabs from "./components/DsTabs";

Content of Tab Pane 1

Button111

Content of Tab Pane 2

39e08bf9c7d5

props

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值