react第三方組件庫_如何自定義您的第三方React組件

react第三方組件庫

by Jacob Goh

雅各布·高

如何自定義您的第三方React組件 (How to customize your third party React components)

Component libraries make our lives easier.

組件庫使我們的生活更輕松。

But as developers, you might often find yourselves in situations where third party components don’t provide the functionality or customization capability the project needs.

但是作為開發人員,您經常會在第三方組件無法提供項目所需的功能或自定義功能的情況下發現自己。

We are left with 2 choices:

我們有2個選擇:

  1. Write the component from scratch yourself

    自己從頭開始編寫組件
  2. Customize the third party components

    定制第三方組件

What to choose depends on the component and the situation that you are in.

選擇什么取決于組件和您所處的情況。

Apparently, some components are not customizable, and some feature requirements are not feasible. But most of the time, customizing third party components is the less time-consuming option. Here’s how.

顯然,某些組件不可定制,并且某些功能要求不可行。 但是在大多數情況下,自定義第三方組件是耗時較少的選擇。 這是如何做。

開始之前 (Before we start)

For example, we are going to customize the react-bootstrap-typeahead component.

例如,我們將自定義react-bootstrap-typeahead組件。

Here’s the starter if you want to follow along https://stackblitz.com/edit/react-hznpca

如果您想遵循https://stackblitz.com/edit/react-hznpca的方法,這是入門

1.覆蓋CSS (1. Overwriting CSS)

This is fairly straightforward.

這很簡單。

Just find out what the component’s CSS classes are and overwrite them with new CSS.

只需找出組件CSS類是什么,然后用新CSS覆蓋它們即可。

(Example)

Goal: Add a dropdown icon to the input box, so that it looks like a drop-down.

目標:在輸入框中添加一個下拉圖標,使其看起來像一個下拉菜單。

Just add Font Awesome to index.html

只需將Font Awesome添加到index.html

and add this CSS to style.css

并將此CSS添加到style.css

Demo: https://stackblitz.com/edit/react-wdjptx

演示: https : //stackblitz.com/edit/react-wdjptx

2.包裝器組件 (2. Wrapper Component)

This is where you can alter the default behavior of the third party component.

在這里您可以更改第三方組件的默認行為。

Start by creating a wrapper component CustomizedTypeahead and replace Typeahead with it.

首先創建包裝器組件CustomizedTypeahead然后用它替換Typeahead

https://stackblitz.com/edit/react-rwyjmm

https://stackblitz.com/edit/react-rwyjmm

This wrapper component has no effect for now. It’s simply passing props down to the Typeahead component.

該包裝器組件暫時無效。 它只是將props傳遞到Typeahead組件。

We are going to customize the component behavior by making changes to props.

我們將通過更改props來定制組件的行為。

示例:設置默認道具 (Example: Setting Default Props)

Goal: Adding default props

目標:添加默認道具

Let’s start with the simplest customization.

讓我們從最簡單的自定義開始。

Let’s say we want all the CustomizedTypeahead to have the clearButton props enabled by default.

假設我們希望所有CustomizedTypeahead都默認啟用clearButton道具。

We can do so by:

我們可以這樣做:

This is equivalent to:

這等效于:

We create injectedProps and will put all the props modification inside to make the code manageable.

我們創建了injectedProps ,并將所有props修改放入其中以使代碼易于管理。

Demo: https://stackblitz.com/edit/react-tk9pau

演示: https : //stackblitz.com/edit/react-tk9pau

示例:修改道具 (Example: Modifying Props)

Goal: To sort all options by alphabetic order

目標:按字母順序對所有選項進行排序

We are receiving options, which is an array of objects, and labelKey, which tells us that the option's label should be optionObject[labelKey]. Our goal is to sort optionObject[labelKey] by alphabetic order.

我們正在接收options (它是對象的數組)和labelKey (它告訴我們選項的標簽應該是optionObject[labelKey] 。 我們的目標是按字母順序對optionObject[labelKey]進行排序。

We can do so by using Array.prototype.sort() to sort the options array.

我們可以使用Array.prototype.sort()對options數組進行排序。

This way, the options in injectedProps will overwrite the original options in props. That's how we can sort all options by alphabetic order by default.

這樣, optionsinjectedProps將覆蓋原來的optionsprops 。 這就是默認情況下我們可以按字母順序對所有選項進行排序的方式。

Demo: https://stackblitz.com/edit/react-cqv5vz

演示: https : //stackblitz.com/edit/react-cqv5vz

示例:攔截事件監聽器 (Example: Intercepting Event Listeners)

Goal: When the user selects an option, if the user has selected both “California” and “Texas” together, alert the user and clear the selection (for no particular reason other than for this demo).

目標:當用戶選擇一個選項時,如果用戶同時選擇了“加利福尼亞”和“德克薩斯”,則警告用戶并清除選擇(除了本演示之外,沒有其他特殊原因)。

This is the fun part where you can do lots of customization.

這是一個有趣的部分,您可以在其中進行很多自定義。

Basically, this is how it will work:

基本上,這就是它的工作方式:

Note the if(onChange) onChange(selectedOptions);. This makes sure that the original onChange event listener continues to run after we intercept it.

注意if(onChange) onChange(selectedOptions); 。 這可以確保原始的onChange事件偵聽器在我們攔截后繼續運行。

Here’s what we did in the code above:

這是我們在上面的代碼中所做的:

  1. We create an onChange function that is of the same structure of the default onChange function. It's a function that receives an array of selected options.

    我們創建一個onChange函數,其功能與默認onChange函數的結構相同。 該函數接收選定選項的數組。

  2. We scan through the selected options and check if it’s valid.

    我們瀏覽選定的選項,并檢查其是否有效。
  3. If it’s invalid, show an alert and clear the input

    如果無效,則顯示警報并清除輸入
  4. Run the original onChange event listener

    運行原始的onChange事件偵聽器

Demo: https://stackblitz.com/edit/react-ravwmw

演示: https : //stackblitz.com/edit/react-ravwmw

3.修改源代碼 (3. Modifying the source code)

Caution: Don’t overuse this! This is your last resort. You should only do this if there is no other choice.

注意:請勿過度使用此功能! 這是您的不得已的方法。 僅當沒有其他選擇時,您才應該這樣做。

If none of the above works for you, the choices you have are now limited to:

如果以上都不適合您,那么您現在只能選擇以下選項:

  • Find another component library

    查找另一個組件庫
  • Write your own component from scratch

    從頭開始編寫自己的組件
  • Modify the component’s source code

    修改組件的源代碼

It’s actually not uncommon that you might have to modify a package’s source code to fit a project’s needs. Especially if you’ve found a bug in a package and you need it fixed urgently.

實際上,您可能不得不修改軟件包的源代碼以適應項目的需求并不少見。 特別是如果您在程序包中發現錯誤,并且需要緊急修復。

But there are a few cons:

但是有一些缺點:

  • Some packages use different languages like CoffeeScript or Typescript. If you don’t know the language, you don’t know how to edit it.

    一些軟件包使用不同的語言,例如CoffeeScript或Typescript。 如果您不懂該語言,就不會編輯。
  • It can be time-consuming to study the source code and figure out where exactly to put your modification.

    研究源代碼并弄清楚將修改確切地放在哪里可能會很耗時。
  • You may unintentionally break some part of the package.

    您可能無意間破壞了包裝的某些部分。
  • When the package updates, you would need to apply the update manually.

    軟件包更新時,您將需要手動應用更新。

If you decide to go ahead and make some modifications to the source code, here’s how.

如果您決定繼續對源代碼進行一些修改,請按以下步驟操作。

1.分叉Github存儲庫 (1. Fork the Github Repository)

In our example case, go to https://github.com/ericgio/react-bootstrap-typeahead and fork the repo to your own GitHub account.

在我們的示例案例中,轉到https://github.com/ericgio/react-bootstrap-typeahead并將存儲庫分叉到您自己的GitHub帳戶。

2.將存儲庫克隆到您的計算機 (2. Clone the repo to your machine)

3.進行修改 (3. Make the modification)

4.將存儲庫推送到您的GitHub帳戶 (4. Push the repo to your GitHub account)

5.安裝您的倉庫作為依賴 (5. Install your repo as a dependency)

After you fork the repo, your GitHub repo’s URL should be https://github.com/<your GitHub username>/react-bootstrap-typeahead.

在分叉存儲庫之后,您的GitHub存儲庫的URL應該為https://github.com/<your GitHub username>/react-bootstrap-typ eahead。

You can install this git repo as a dependency by executing this command:

您可以通過執行以下命令將此git repo安裝為依賴項:

npm i https://github.com/<your GitHub username>/react-bootstrap-typeahead

After installation, you should see this in package.json:

安裝后,您應該在package.json中看到以下內容:

"dependencies": {     "react-bootstrap-typeahead": "git+https://github.com/<your github username>/react-bootstrap-typeahead.git" }

結論 (Conclusion)

We talked about three ways to customize your third party React components.

我們討論了自定義第三方React組件的三種方法。

  1. Overwriting CSS

    覆蓋CSS
  2. Using Wrapper Component

    使用包裝器組件
  3. Modifying the source code

    修改源代碼

Hopefully, this makes your life as a React developer easier.

希望這會使您作為React開發人員的生活更加輕松。

In the meantime, let’s all take a moment and be grateful to all the open source creators/contributors out there. Without these open source packages, we wouldn’t be able to move as fast as we do today.

同時,讓我們花點時間感謝所有開放源代碼創建者/貢獻者。 沒有這些開源軟件包,我們將無法像今天一樣快。

What’s your experience with third party component libraries? What other methods would you use to customize them? Leave a comment!

您對第三方組件庫有何經驗? 您還將使用其他哪些方法來自定義它們? 發表評論!

Originally published at dev.to.

最初發布于dev.to。

翻譯自: https://www.freecodecamp.org/news/how-to-customize-your-third-party-react-components-e0afd88532c9/

react第三方組件庫

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392511.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392511.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392511.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

gcp devops_將GCP AI平臺筆記本用作可重現的數據科學環境

gcp devopsBy: Edward Krueger and Douglas Franklin.作者&#xff1a; 愛德華克魯格 ( Edward Krueger)和道格拉斯富蘭克林 ( Douglas Franklin) 。 In this article, we will cover how to set up a cloud computing instance to run Python with or without Jupyter Notebo…

迅為工業級iMX6Q開發板全新升級兼容PLUS版本|四核商業級|工業級|雙核商業級...

軟硬件全面升級 1. 新增Yocto項目的支持 增加opencv等軟件功能 2. 新近推出i.MX6增強版本核心板&#xff08;PLUS&#xff09; -性能更強 四種核心板全兼容 四核商業級2G/16G&#xff1b;雙核商業級1G/8G &#xff1b;四核工業級1G/8G &#xff1b;四核增強版(PLUS) 3. 豪華配…

flume 中的 hdfs sink round 和roll

http://blog.csdn.net/kntao/article/details/49278239 http://flume.apache.org/FlumeUserGuide.html#exec-source 默認的是是SequenceFile所以數據存在hdfs上通過命令查看的時候會是亂碼,如果此時需要修改filetype和writeFormat來修改 hdfs.fileTypeSequenceFileFile format:…

leetcode 649. Dota2 參議院(貪心算法)

Dota2 的世界里有兩個陣營&#xff1a;Radiant(天輝)和 Dire(夜魘) Dota2 參議院由來自兩派的參議員組成。現在參議院希望對一個 Dota2 游戲里的改變作出決定。他們以一個基于輪為過程的投票進行。在每一輪中&#xff0c;每一位參議員都可以行使兩項權利中的一項&#xff1a; …

電力現貨市場現貨需求_現貨與情緒:現貨銅市場中的自然語言處理與情緒評分

電力現貨市場現貨需求Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works with…

PHP學習系列(1)——字符串處理函數(2)

6、chunk_split() 函數把字符串分割為一連串更小的部分。本函數不改變原始字符串。 語法&#xff1a;chunk_split(string,length,end) 參數&#xff1a; string——必需。規定要分割的字符串。 length——可選。一個數字&#xff0c;定義字符串塊的長度。 end——可選。字符串值…

java做主成分分析_主成分分析PCA

PCA(Principal Component Analysis)&#xff0c;即主成分分析&#xff0c;一種常用于數據降維分析的方法。要理解PCA的原理&#xff0c;首先需要理解矩陣變換的意義。矩陣變換&#xff0c;有兩種意義&#xff1a;1&#xff0c;在當前坐標系下的向量&#xff0c;經過矩陣M變換后…

個人學習進度(第十六周)

轉載于:https://www.cnblogs.com/lhj1017/p/7011993.html

什么叫靜態構建版本號碼_為什么要使用GatsbyJS構建靜態網站

什么叫靜態構建版本號碼by Ajay NS由Ajay NS 為什么要使用GatsbyJS構建靜態網站 (Why you should use GatsbyJS to build static sites) Gatsby has been growing over time, and I’m glad to see it in use by a huge number of sites like marketing sites, blogs, and gen…

leetcode 217. 存在重復元素

給定一個整數數組&#xff0c;判斷是否存在重復元素。 如果任意一值在數組中出現至少兩次&#xff0c;函數返回 true 。如果數組中每個元素都不相同&#xff0c;則返回 false 。 示例 1: 輸入: [1,2,3,1] 輸出: true 代碼 class Solution {public boolean containsDuplica…

C#正則表達式提取HTML中IMG標簽的URL地址 .

/// <summary> /// 取得HTML中所有圖片的 URL。 /// </summary> /// <param name"sHtmlText">HTML代碼</param> /// <returns>圖片的URL列表</returns> public static string[] GetHtmlImageUrlList(string sHtmlText) { // 定…

java datarow 使用_DataRow中的鏈接(數據表)

我正在動態構建一個DataTable&#xff0c;我正在嘗試在DataRow中添加一個“鏈接”&#xff0c;我將其添加到DataTable中 . DataTable在創建后綁定到GridView .像這樣的東西&#xff1a;DataTable dataTable new DataTable();foreach (Item item in items){DataRow row dataTa…

mac、windows如何強制關閉tomcat進程

方式1.打開cmd&#xff0c;或mac的終端&#xff0c;輸入&#xff1a;① ps aux | grep "tomcat"&#xff0c;找到響應的進程id&#xff1b;② kill -9 查詢的id&#xff0c;來強制關閉進程方式2&#xff1a;window&#xff0c;打開tomcat文件夾 --> bin --> sh…

用python繪制箱線圖_用衛星圖像繪制世界海岸線圖-第一部分

用python繪制箱線圖At the UKHO, we use data science to gain valuable insight into the data sets we hold and further our understanding of the marine environment around us.在UKHO&#xff0c;我們使用數據科學獲得對所擁有數據集的寶貴見解&#xff0c;并進一步了解周…

vue 遞歸創建菜單_如何在Vue中創建類似中等的突出顯示菜單

vue 遞歸創建菜單by Taha Shashtari由Taha Shashtari 如何在Vue中創建類似中等的突出顯示菜單 (How to Create a Medium-Like Highlight Menu in Vue) A cool feature in Medium is the highlight menu that pops up when you select some text. This menu contains buttons t…

leetcode 376. 擺動序列(dp)

如果連續數字之間的差嚴格地在正數和負數之間交替&#xff0c;則數字序列稱為擺動序列。第一個差&#xff08;如果存在的話&#xff09;可能是正數或負數。少于兩個元素的序列也是擺動序列。 例如&#xff0c; [1,7,4,9,2,5] 是一個擺動序列&#xff0c;因為差值 (6,-3,5,-7,3…

在ASP.NET Atlas中調用Web Service——創建Mashup調用遠端Web Service(基礎知識以及簡單示例)...

作者&#xff1a;Dflying Chen &#xff08;http://dflying.cnblogs.com/&#xff09; 注&#xff1a;Atlas中的Mashup極其復雜&#xff0c;其中涉及眾多的對象與架構&#xff0c;為了寫這篇文章&#xff0c;我花了不少時間學習研究。同時&#xff0c;關于這方面資源的匱乏簡直…

java彈框形式輸入_java中點擊一個按鈕彈出兩個輸入文本框的源代碼

展開全部寫了一個很簡單的案例,可以參考和修改import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import…

sap wm內向交貨步驟_內向型人在數據科學中成功的五個有效步驟

sap wm內向交貨步驟Just like most attributes of humans, including both the bright and dark sides, being an introvert is no exception. This article was not written to inspire you as most articles about data science or engineering do. What we want is that by …

C# 學習之路--百度網盤爬蟲設計與實現(一)

百度網盤爬蟲 現在市面上出現了很多網盤搜索引擎&#xff0c;寫這系列博文及爬蟲程序的初衷&#xff1a; 更方面的查找資源學習C#學習爬蟲的設計與實現記錄學習歷程自我監督 能力有限&#xff0c;如有不妥之處&#xff0c;還請各位看官點評。同在學習的網友~與君共勉。工具/庫選…