Create a Modular React Component Library

栏目: IT技术 · 发布时间: 3年前

Create a Modular React Component Library

A guide to building a React component library that scales

Create a Modular React Component Library

Mar 31 ·6min read

Create a Modular React Component Library

Design systems/UI libraries used to be a luxury enjoyed exclusively by large enterprises, but in recent years, due to the advent of new technologies , we've seen that changed quite drastically. Design systems have now become a tool used by organizations of all scales, from single freelance developers and small dev teams, all the way to large and mature enterprises.

Why Build a UI Libray/Design System?

UI libraries affect both ends of the development process: the coding and the final product.

UI libraries speed up development

  • UI libraries maximize code reuse and speed up development. They do so by making components available to all projects in and across repositories. They also make components easier to find and understand (as they’re documented and indexed in one central location) — after all, reusable code no one knows about isn’t that useful.
  • UI libraries make codebases easier to scale and maintain. A DRY codebase means fewer lines and files to go through. Fewer duplications also mean better predictability to changes in code (it may be argued that sharing code across projects makes predicting outcomes more challenging, but as you’ll see in this post, it’s only a matter of using the right tools).
  • Lastly, code that is written for reuse is better code. It’s documented, testable, and pure (return values for each argument remain consistent with zero changes to their environment). Better code is more maintainable code.

UI libraries help deliver a betteruser experience

  • A limited set of UI components means your users spend less time learning your product and more time enjoying it.
  • Consistency in style and functionality makes it much easier for users to predict the outcomes of actions performed on your app, minimizing the chance for confusion and the frustration that follows it.

What’s a Modular Library? Why Are They Important?

A standard library is a monolith. It’s built and maintained as a single object — a single repository, a single build setup, and, usually, a single package.

Components are organized by ownership or business functionality

A modular library, or a collection, is a scope that serves only as a way to organize components by ownership or business functionality (there are currently no other implementations of modular libraries that I’m aware of other than Bit.dev , but I’m sure there will be).

Each component is versioned and built as independent code

Components published to a collection are versioned and built independently, which means they can be harvested from any codebase/repository. That gives us the freedom to publish components whenever we like and from whatever project we choose. It doesn’t have to be a full-on component-library project.

No pesky meaningless updates

Consumers of shared components only have to deal with version updates that are relevant to what they actually use as they consume each component directly and not as part of a larger project (e.g., a library).

Building a UI Library With Bit and Bit.dev

What is Bit/Bit.dev?

Bit.dev is a component hub (a private registry and a documentation site). It’s where you host, document, and manage reusable components from your different projects. It works perfectly with Bit , an open-source tool that handles component isolation and publishing (when using Bit.dev , components are published to Bit’s registry).

Create a Modular React Component Library
Example: Browsing through component collections in Bit.dev

As mentioned earlier, components can be published to a collection from any project at all. To demonstrate that, I’ll harvest and publish components from a demo React with TS app.

Create a Modular React Component Library
https://github.com/giteden/react-ts-demo-app

1. Create a component collection in Bit.dev .

2. Install Bit globally (npm/Yarn).

$ yarn global add bit-bin

3. Log into your account.

$ bit login

4. Initialize a workspace in the project’s directory.

// To follow along with my demo app, clone and install it$ git clone https://github.com/giteden/react-ts-demo-app.git
$ cd react-ts-demo-app
$ yarn
// initialize a workspace (in your project's directory)$ bit init --package-manager yarn

Note:Notice how a new .bitmap file has been added to your directory, and a bit section has been added to your package.json .

5. Track all components. In my case, components are located in the components directory (for example, src/components/button/index.js ):

$ bit add src/components/*

Note:Placing all files under the same directory is yet another way to make your code more comprehensible to other developers (maintainers and consumers of your reusable components). It’s much easier to understand how different files relate to each other when they’re grouped under the same directory. Moreover, it’s a way to make your reusable component more mobile and autonomous.

6. Configure a compiler to make the components usable in other environments with different setups:

$ bit import bit.envs/compilers/react-typescript --compiler

Note:For other compilers, check out Bit’s ENVs collection or learn how to build your own .

7. Tag the components to build them in isolation (and lock changes):

$ bit tag --all 1.0.0

Note:Bit tracks your component dependencies by itself and doesn’t require manual configuration. Having said that, it’s good practice to check for unresolved dependency issues using $ bit status .

8. Export all tracked components (push them to the shared collection):

$ bit export <user-name>.<collection-name>

The components are now shared and publicly available :+1:

If you followed along, go to bit.dev/<user-name>/<collection-name> to see them rendered in Bit’s playground. Otherwise, you can check out my collection here:

Create a Modular React Component Library
A demo React with TS collection

Documenting Components

The process of documentation starts at writing components in your local dev environment and ends at writing examples in the component page on Bit.dev.

When using React with TypeScript, most of the job is done for us. Bit extracts props and types, forms documentation, and displays it on the component page (on B it.dev ). As wonderful as it is, it’s always good to enrich your autogenerated documentation with JSDocs descriptions.

Create a Modular React Component Library

Documentation on the component page

Note:When using react-docgen / Bit.dev to autogenerate docs for your reusable components, the prop’s type/interface should be defined both as a generic of React.FC<T> and (directly) as the function’s arguments type (otherwise, your props won’t appear in the generated docs).

Writing Examples in Bit’s Playground

After exporting components, it’s best to complete the documentation process by providing an example (on the component page).

Examples enable components to render in Bit’s playground (a component that doesn’t receive its required props won’t be able to render) and guide consumers of components on how/how best to use them.

Create a Modular React Component Library

A component rendered in Bit’s playground

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编写高质量代码

编写高质量代码

秦小波 / 机械工业出版社华章公司 / 2011-12-28 / 59.00元

在通往“Java技术殿堂”的路上,本书将为你指点迷津!内容全部由Java编码的最佳实践组成,从语法、程序设计和架构、工具和框架、编码风格和编程思想等五大方面对Java程序员遇到的各种棘手的疑难问题给出了经验性的解决方案,为Java程序员如何编写高质量的Java代码提出了151条极为宝贵的建议。对于每一个问题,不仅以建议的方式从正反两面给出了被实践证明为十分优秀的解决方案和非常糟糕的解决方案,而且还......一起来看看 《编写高质量代码》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试