广告
返回顶部
首页 > 资讯 > 精选 >基于React封装一个组件的方法是什么
  • 657
分享到

基于React封装一个组件的方法是什么

2023-06-29 00:06:20 657人浏览 八月长安
摘要

这篇文章主要讲解了“基于React封装一个组件的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于React封装一个组件的方法是什么”吧!antd 是如何封装组件的仓库地址divi

这篇文章主要讲解了“基于React封装一个组件的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于React封装一个组件的方法是什么”吧!

    antd 是如何封装组件的

    仓库地址

    • divider 组件在下图对应目录下 (代码我会拷贝过来,感兴趣的还是可以去克隆一下仓库)

    基于React封装一个组件的方法是什么

    divider 组件源代码

    antd 的源码使用了 typescript 语法,因此不了解语法的同学要及时了解哦!

    import * as React from 'react';import classNames from 'classnames';import { ConfiGConsumer, ConfigConsumerProps } from '../config-provider';export interface DividerProps {    prefixCls?: string;    type?: 'horizontal' | 'vertical';    orientation?: 'left' | 'right' | 'center';    className?: string;    children?: React.Reactnode;    dashed?: boolean;    style?: React.CSSProperties;    plain?: boolean;}const Divider: React.FC<DividerProps> = props => (    <ConfigConsumer>        {({ getPrefixCls, direction }: ConfigConsumerProps) => {            const {                prefixCls: customizePrefixCls,                type = 'horizontal',                orientation = 'center',                className,                children,                dashed,                plain,                ...restProps            } = props;            const prefixCls = getPrefixCls('divider', customizePrefixCls);            const orientationPrefix = orientation.length > 0 ? `-${orientation}` : orientation;            const hasChildren = !!children;            const classString = classNames(                prefixCls,                `${prefixCls}-${type}`,                {                    [`${prefixCls}-with-text`]: hasChildren,                    [`${prefixCls}-with-text${orientationPrefix}`]: hasChildren,                    [`${prefixCls}-dashed`]: !!dashed,                    [`${prefixCls}-plain`]: !!plain,                    [`${prefixCls}-rtl`]: direction === 'rtl',                },                className,            );            return (                <div className={classString} {...restProps} role="separator">                    {children && <span className={`${prefixCls}-inner-text`}>{children}</span>}                </div>            );        }}    </ConfigConsumer>);export default Divider;

    如何暴露组件属性

    在源码中,最先看到的是以下内容,这些属性也就是divider组件所暴露的属性,我们可以 <Divider type='vertical' /> 这样来传入 type 属性,那么 divider 分割线样式就会渲染为垂直分割线,是不是很熟悉!

    export interface DividerProps { // interface 是 TypeScript 的语法    prefixCls?: string;    type?: 'horizontal' | 'vertical'; // 限定 type 只能传入两个值中的一个    orientation?: 'left' | 'right' | 'center';    className?: string;    children?: React.ReactNode;    dashed?: boolean;    style?: React.CSSProperties;    plain?: boolean;}

    在上面的属性中,我们还发现 className 和 style是比较常见的属性,这代表我们可以 <Divider type='vertical' className='myClassName' style={{width: '1em'}} /> 这样使用这些属性。

    如何设置统一类名前缀

    我们知道,antd 的组件类名会有他们独特的前缀 ant-,这是如何处理的呢?继续看源码。

    <ConfigConsumer>    {({ getPrefixCls, direction }: ConfigConsumerProps) => {        const {            prefixCls: customizePrefixCls,            type = 'horizontal',            orientation = 'center',            className,            children,            dashed,            plain,            ...restProps        } = props;        const prefixCls = getPrefixCls('divider', customizePrefixCls);

    从源码中,我们发现 prefixCls ,这里是通过 getPrefixCls 方法生成,再看看 getPrefixCls 方法的源码,如下。

    export interface ConfigConsumerProps {  ...  getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => string;  ...}const defaultGetPrefixCls = (suffixCls?: string, customizePrefixCls?: string) => {  if (customizePrefixCls) return customizePrefixCls;  return suffixCls ? `ant-${suffixCls}` : 'ant';};

    不难发现此时会生成的类名前缀为 ant-divider 。

    如何处理样式与类名

    我们封装的组件肯定是有预设的样式,又因为样式要通过类名来定义,而我们传入的属性值则会决定组件上要添加哪个类名,这又是如何实现的呢?下面看源码。

    import classNames from 'classnames';const classString = classNames(    prefixCls,    `${prefixCls}-${type}`,    {        [`${prefixCls}-with-text`]: hasChildren,        [`${prefixCls}-with-text${orientationPrefix}`]: hasChildren,        [`${prefixCls}-dashed`]: !!dashed,        [`${prefixCls}-plain`]: !!plain,        [`${prefixCls}-rtl`]: direction === 'rtl',    },    className,);return (    <div className={classString} {...restProps} role="separator">        {children && <span className={`${prefixCls}-inner-text`}>{children}</span>}    </div>);

    我们发现,它通过 classNames 方法)定义了一个所有类名的常量,然后传给了 div 中的 className 属性。

    其实生成的类名也就是 ant-divider-horizontal 这个样子,那么css中以此类名定义的样式也就自然会生效了。而 className 和 style 属性则是通过 {...restProps} 来传入。

    最后我们再看看它的css样式代码是怎么写的!

    divider 组件样式源代码

    antd 组件的样式使用 Less 书写,不了解 Less 语法的同学一定要了解一下。

    @import '../../style/themes/index';@import '../../style/mixins/index';@divider-prefix-cls: ~'@{ant-prefix}-divider'; // 可以看到这里对应的也就是之前说到的类名前缀.@{divider-prefix-cls} {  .reset-component();  border-top: @border-width-base solid @divider-color;  &-vertical { // 这里的完整类名其实就是 ant-divider-vertical, 也就是 divider 组件的 type 属性值为 vertical 时对应的样式    position: relative;    top: -0.06em;    display: inline-block;    height: 0.9em;    margin: 0 8px;    vertical-align: middle;    border-top: 0;    border-left: @border-width-base solid @divider-color;  }  &-horizontal {    display: flex;    clear: both;    width: 100%;    min-width: 100%;     margin: 24px 0;  }  &-horizontal&-with-text {    display: flex;    margin: 16px 0;    color: @heading-color;    font-weight: 500;    font-size: @font-size-lg;    white-space: nowrap;    text-align: center;    border-top: 0;    border-top-color: @divider-color;    &::before,    &::after {      position: relative;      top: 50%;      width: 50%;      border-top: @border-width-base solid transparent;      // Chrome not accept `inherit` in `border-top`      border-top-color: inherit;      border-bottom: 0;      transfORM: translateY(50%);      content: '';    }  }  &-horizontal&-with-text-left {    &::before {      top: 50%;      width: @divider-orientation-margin;    }    &::after {      top: 50%;      width: 100% - @divider-orientation-margin;    }  }  &-horizontal&-with-text-right {    &::before {      top: 50%;      width: 100% - @divider-orientation-margin;    }    &::after {      top: 50%;      width: @divider-orientation-margin;    }  }  &-inner-text {    display: inline-block;    padding: 0 @divider-text-padding;  }  &-dashed {    background: none;    border-color: @divider-color;    border-style: dashed;    border-width: @border-width-base 0 0;  }  &-horizontal&-with-text&-dashed {    border-top: 0;    &::before,    &::after {      border-style: dashed none none;    }  }  &-vertical&-dashed {    border-width: 0 0 0 @border-width-base;  }  &-plain&-with-text {    color: @text-color;    font-weight: normal;    font-size: @font-size-base;  }}@import './rtl';

    感谢各位的阅读,以上就是“基于React封装一个组件的方法是什么”的内容了,经过本文的学习后,相信大家对基于React封装一个组件的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

    --结束END--

    本文标题: 基于React封装一个组件的方法是什么

    本文链接: https://www.lsjlt.com/news/321424.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • 基于React封装一个组件的方法是什么
      这篇文章主要讲解了“基于React封装一个组件的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于React封装一个组件的方法是什么”吧!antd 是如何封装组件的仓库地址divi...
      99+
      2023-06-29
    • 你知道怎么基于 React 封装一个组件吗
      目录antd 是如何封装组件的仓库地址divider 组件源代码如何暴露组件属性如何设置统一类名前缀如何处理样式与类名divider 组件样式源代码总结前言 很多小伙伴在第一次尝试封...
      99+
      2022-11-13
    • react结合typescript封装组件的方法是什么
      今天小编给大家分享一下react结合typescript封装组件的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。项...
      99+
      2023-07-06
    • 基于React封装组件的实现步骤是怎样的
      这篇文章将为大家详细讲解有关基于React封装组件的实现步骤是怎样的,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。前言很多小伙伴在第一次尝试封装组件时会和我一样碰到许多问题,比如人家的组件会...
      99+
      2023-06-21
    • 基于Java递归算法的封装解决方法是什么
      本篇内容介绍了“基于Java递归算法的封装解决方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、递归算法1、概念简介递归算法的核心...
      99+
      2023-06-02
    • 基python+selenium的二次封装方法是什么
      本篇内容主要讲解“基python+selenium的二次封装方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“基python+selenium的二次封装方法是什么”吧!  首先在根目录下创...
      99+
      2023-06-02
    • react获取数组最后一个元素的方法是什么
      可以使用数组的`length`属性和索引来获取数组的最后一个元素。具体的方法有两种:1. 使用索引:使用数组的`length`属性减...
      99+
      2023-10-07
      react
    • vue封装自定义分页器组件与使用方法是什么
      这篇文章给大家介绍vue封装自定义分页器组件与使用方法是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。前言分页是开发各种系统时候最常用的功能,下面为本人封装的一个分页组件。实现分页器操作需要以下参数当前页: pag...
      99+
      2023-06-26
    • 基于Python搭建个人云盘的方法是什么
      这篇文章主要介绍“基于Python搭建个人云盘的方法是什么”,在日常操作中,相信很多人在基于Python搭建个人云盘的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于Python搭建个人云盘的方法...
      99+
      2023-06-16
    • uni-app组件的基本使用方法是什么
      本文小编为大家详细介绍“uni-app组件的基本使用方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“uni-app组件的基本使用方法是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1.组件概念首先讲...
      99+
      2023-07-05
    • React组件的创建与state同步异步方法是什么
      这篇文章主要介绍“React组件的创建与state同步异步方法是什么”,在日常操作中,相信很多人在React组件的创建与state同步异步方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”React组件...
      99+
      2023-07-05
    • php遍历一个数组的三种方法是什么
      3种遍历数组的方法:1、用for语句遍历,语法“for($i=0;$i<数组长度;$i++){//循环代码}”;2、用foreach语句遍历,语法“foreach($arr as $k=>$v){//循环代码}”;3、用whil...
      99+
      2022-09-06
    • python两个一维数组合成二维数组的方法是什么
      在Python中,可以使用zip()函数将两个一维数组合并成一个二维数组。zip()函数接受任意数量的可迭代对象作为参数,并返回一个...
      99+
      2023-09-06
      python
    • java多线程读取同一个文件的方法是什么
      在Java中,可以使用多线程读取同一个文件的方法有两种:1. 使用多个线程分别读取文件的不同部分:这种方法需要将文件分成多个部分,每...
      99+
      2023-09-13
      java
    • WPF中鼠标/键盘/拖拽事件及用行为封装事件的方法是什么
      这篇“WPF中鼠标/键盘/拖拽事件及用行为封装事件的方法是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“WPF中鼠标/键...
      99+
      2023-07-05
    • Linux系统一次重命名多个文件的方法是什么
      本篇文章为大家展示了Linux系统一次重命名多个文件的方法是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。mmv 程序可在基于 Debian 的系统的默认仓库中使用。 要想在 Debian、Ub...
      99+
      2023-06-28
    • LNMP一键安装包清理mysql-bin.00001日志文件的方法是什么
      本篇文章给大家分享的是有关LNMP一键安装包清理mysql-bin.00001日志文件的方法是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧...
      99+
      2022-10-19
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作