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第三方组件库

react第三方组件库_如何自定义您的第三方React组件相关推荐

  1. 引入antd组件样式_如何使用 dumi 和 fatherbuild 创建组件库

    在这个文章中,将会简单的介绍如何使用 dumi 和 father-build 来创建组件库和维护组件库.最终我们会演示如何组织组件库的代码.还会输出一个简单的组件库. 脚手架初始化 新建一个空文件夹, ...

  2. Vue常用的组件库大全【前端工程师必备】【实时更新】【移动端、PC端(web端)、数据可视化组件库(数据大屏) 、动画组件库、3D组件库】

    Vue常用的组件库大全[前端工程师必备] (一)移动端 常用组件库 1)Vant ui 2)Cube UI 3)VUX 4) NuTUI 5)Mint ui 6)Varlet UI 7)OnsenUI ...

  3. 苹果设计组件库_建立设计系统和组件库

    苹果设计组件库 This post is based on the series of posts: Modernizing a jQuery frontend with React. If you ...

  4. Vue.js_04_组件_Element组件库_组件通信_PropsDown_EventsUp

    Vue.js 四天课程学习笔记_第4天 课程内容概要: 1. 前情回顾 2. 通过demo再次复习一下 计算属性 是如何生成 过滤后的数组 3. Vue实例的生命周期(11个钩子) 4. 介绍组件化思 ...

  5. python 升级所有库_自动更新Python所有第三方库

    一般python用得比较久以后,就会安装很多第三方的库.比如这是我的pip list情况: pip list 而且一屏还显示不完. 通过如下命令可以看到需要更新的第三方库: pip list -o 需 ...

  6. vue怎么自己创建组件并引用_如何在组件库项目内直接引用vue-cli生成的组件库文件...

    感谢大家帮忙, 我第一次用vue-cli制作组件库,使用的命令是: vue-cli-service build --target lib --name vpui ./src/components/in ...

  7. Vant 2 - 移动端 Vue 组件库 _ 问题记录

    目录 Popup 弹出层 DatetimePicker 时间选择 Field 输入框 Picker 选择器 List 列表 Tab 标签页 发布初衷 : 记录在移动端项目中使用 Vant  2 组件库 ...

  8. react native多语言_前端福音:为什么使用 React 和 SVG 开发图形 UI 是天作之合?

    本文最初发布于 Data Language 网站,经网站授权由 InfoQ 中文站翻译并分享. React 和 SVG 是一种强大的组合:声明式 UI 组件库与声明式图形语言堪称绝配,是前端开发人员的 ...

  9. react学习预备知识_在10分钟内学习React基础知识

    react学习预备知识 If you want to learn the basics of React in the time it takes you to drink a cup of coff ...

最新文章

  1. Spring MVC测试框架
  2. is 和 == 区别 编码和解码
  3. 软件测试自学钢琴考级,钢琴考级被音基难倒?不要慌,跟着这款钢琴陪练APP一起练...
  4. python解决鸡兔同笼_Python解决鸡兔同笼问题的方法
  5. 【C#控件详解】对话框类控件(打开文件,保存文件,选择字体和颜色)
  6. 互联网寒冬!“996”为什么还没实行?我还等着早点下班呢!
  7. wp config.php mysql_WordPress手动配置wp-config.php文件
  8. css3 中background的新增加的属性的用法(一)
  9. hashmap删除指定key_「集合系列」- 深入浅出分析HashMap
  10. 为何各家抢滩物联网?
  11. 用动态规划解决最长公共子序列
  12. ArrayList 与 LinkedList 插入、查询效率测试
  13. c语言 mysql 查询数字_c语言mysql查询数据库
  14. Linux下Wordpress建站Guide
  15. Linux 脚本删除大于指定大小的文件
  16. 优云软件又双叒通过CMMI ML3评估 , 研发和质量管理水平创新高
  17. 在微信小程序中使用条形码生成器
  18. python爬虫毕业论文大纲参考模板_毕业论文大纲参考模板.docx
  19. 金蝶移动bos开发教程_移动BOS开发 -- 移动表单
  20. 读书笔记 | 《人间处方》夏目漱石写给青年的信

热门文章

  1. 做了6年的Java,mysql配置环境变量mac
  2. java开发实战经典答案百度云,含面试题+答案
  3. mysql数据库引擎怎么看,值得收藏!
  4. 【工作感悟】达内java大数据课程
  5. 使用IDEA创建Maven项目和Maven使用入门(配图详解)
  6. mysql复制主从集群搭建
  7. 如何对数据库中的表以及表中的字段进行重命名
  8. Windbg命令学习6(!runaway和~)
  9. Rancher中的服务升级实验
  10. 基于easyui开发Web版Activiti流程定制器详解(二)——文件列表