最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

你知道怎么基于?React?封裝一個組件嗎

 更新時間:2022年01月25日 16:44:24   作者:孤影的博客  
這篇文章主要為大家詳細介紹了React如何封裝一個組件,使用antd的divider組件來講解,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

很多小伙伴在第一次嘗試封裝組件時會和我一樣碰到許多問題,比如人家的組件會有 color 屬性,我們在使用組件時傳入組件文檔中說明的屬性值如 primary ,那么這個組件的字體顏色會變?yōu)?primary 對應的顏色,這是如何做到的?還有別人封裝的組件類名都有自己獨特的前綴,這是如何處理的呢,難道是 css 類名全部加上前綴嗎,這也太麻煩了!

如果你正在困惑這些問題,你可以看看這篇文章。

我會參照 antd的divider組件 來講述如何基于React封裝一個組件,以及解答上述的一些問題,請耐心看完!

antd 是如何封裝組件的

倉庫地址

  • divider 組件在下圖對應目錄下 (代碼我會拷貝過來,感興趣的還是可以去克隆一下倉庫)

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;

如何暴露組件屬性

在源碼中,最先看到的是以下內(nèi)容,這些屬性也就是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;
}

在上面的屬性中,我們還發(fā)現(xiàn) className 和 style是比較常見的屬性,這代表我們可以 <Divider type='vertical' className='myClassName' style={{width: '1em'}} /> 這樣使用這些屬性。

如何設置統(tǒng)一類名前綴

我們知道,antd 的組件類名會有他們獨特的前綴 ant-,這是如何處理的呢?繼續(xù)看源碼。

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

從源碼中,我們發(fā)現(xiàn) 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';
};

不難發(fā)現(xiàn)此時會生成的類名前綴為 ant-divider 。

如何處理樣式與類名

我們封裝的組件肯定是有預設的樣式,又因為樣式要通過類名來定義,而我們傳入的屬性值則會決定組件上要添加哪個類名,這又是如何實現(xiàn)的呢?下面看源碼。

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

我們發(fā)現(xiàn),它通過 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';

這樣一來,我相信同學們也大概了解如何去封裝一個組件以及關(guān)鍵點了,在源碼中還有很多地方值得我們學習,比如這里的 ConfigConsumer 的定義與使用,感興趣的同學歡迎一起交流。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!    

相關(guān)文章

  • React路由動畫切換實現(xiàn)過程詳解

    React路由動畫切換實現(xiàn)過程詳解

    這篇文章主要介紹了react-router 路由切換動畫的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2022-12-12
  • 詳解React服務端渲染從入門到精通

    詳解React服務端渲染從入門到精通

    這篇文章主要介紹了詳解React服務端渲染從入門到精通,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • React+hook實現(xiàn)聯(lián)動模糊搜索

    React+hook實現(xiàn)聯(lián)動模糊搜索

    這篇文章主要為大家詳細介紹了如何利用React+hook+antd實現(xiàn)聯(lián)動模糊搜索功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-02-02
  • React自定義Hook-useForkRef的具體使用

    React自定義Hook-useForkRef的具體使用

    本文主要介紹了React自定義Hook-useForkRef的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 如何使用React構(gòu)建一個高效的視頻上傳組件

    如何使用React構(gòu)建一個高效的視頻上傳組件

    本文介紹了如何使用React構(gòu)建一個高效的視頻上傳組件,包括基礎概念、常見問題與解決方案以及易錯點,通過實際代碼案例,展示了如何實現(xiàn)文件大小和格式驗證、上傳進度顯示等功能,并詳細解釋了跨域請求和并發(fā)上傳控制等技術(shù)細節(jié)
    2025-01-01
  • React中props使用介紹

    React中props使用介紹

    props是組件(包括函數(shù)組件和class組件)間的內(nèi)置屬性,用其可以傳遞數(shù)據(jù)給子節(jié)點,props用來傳遞參數(shù)。組件實例化過程中,你可以向其中傳遞一個參數(shù),這個參數(shù)會在實例化過程中被引用
    2022-12-12
  • React中useState和useEffect的用法詳解

    React中useState和useEffect的用法詳解

    Hooks?發(fā)布之后,函數(shù)組件能擁有自己的?state,React提供了很多內(nèi)置的Hooks,這篇文章就來和大家介紹一下useState?和?useEffect的使用,需要的可以參考一下
    2023-06-06
  • 詳解使用React進行組件庫開發(fā)

    詳解使用React進行組件庫開發(fā)

    本篇文章主要介紹了詳解使用React進行組件庫開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 在?React?Native?中給第三方庫打補丁的過程解析

    在?React?Native?中給第三方庫打補丁的過程解析

    這篇文章主要介紹了在?React?Native?中給第三方庫打補丁的過程解析,有時使用了某個React Native 第三方庫,可是它有些問題,我們不得不修改它的源碼,本文介紹如何修改源碼又不會意外丟失修改結(jié)果的方法,需要的朋友可以參考下
    2022-08-08
  • 淺談react前后端同構(gòu)渲染

    淺談react前后端同構(gòu)渲染

    本篇文章主要介紹了淺談react前后端同構(gòu)渲染,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論

璧山县| 蕲春县| 昌都县| 新营市| 金阳县| 西贡区| 丹凤县| 巴彦县| 朝阳区| 寻甸| 七台河市| 宁城县| 比如县| 特克斯县| 翼城县| 蒙自县| 霍城县| 石狮市| 日土县| 浠水县| 兴安盟| 济阳县| 巴青县| 洛浦县| 广饶县| 永仁县| 建德市| 昆山市| 淅川县| 大理市| 晋州市| 塘沽区| 磐安县| 东港市| 内丘县| 师宗县| 聂拉木县| 洛川县| 普定县| 永嘉县| 洪洞县|