Reusing React UI Components
Tags: react, npm, github, javascript
Published: 2019-08-19 14:25:24

##How to leverage [Bit](https://bit.dev) + [NPM](https://www.npmjs.com/) to simplify React and other UI component sharing, reuse and collaboration.

![](https://cdn-images-1.medium.com/max/5760/1*yKaDpHEMugNSn6QR6iRtNA.png)

When working with [UI](https://hackernoon.com/tagged/ui) components, reusability across projects and teams is often the key to faster development, [UI consistency](https://blog.bitsrc.io/building-a-consistent-ui-design-system-4481fb37470f) and simpler maintenance.

As today components are designed to be [reusbale](https://blog.bitsrc.io/building-reusable-react-components-1465a2dccc13) with [React](https://hackernoon.com/tagged/react), Vue and other great frameworks, the ability to reuse them is becoming critical. When different developers are rewriting the same components in different places, time is wasted and maintenance soon becomes a nightmare.


## Component reuse can be tricky

![](https://cdn-images-1.medium.com/max/2000/1*OBpwBuSEKxxkE2Jfu6hyaw.png)

The biggest problem with reusing components is the overhead around it.

First, to make a reusbale component reusbale, you’d have to turn it into a package. This means **Splitting the repo**, **boiler-plating a package** including package.json, build and test configurations etc, and then publish it to NPM.

You’d also have to document the component, create a wiki, and somehow organize all these components and make them discoverable for the team.

The developers using this code will install it with a package manager, which means they can’t make changes to the component when needed without a large amount of overhead. This makes maintenance much harder over time.

Many teams encounter these problems, which might end up leading to undesired forced architectures or code duplications. But, with Bit + NPM you can scale component reuse without this overhead. Here’s how.

## Bit + NPM: Scale component reuse

With a **Bit+NPM workflow** components can be shared, developed and synced across project without the overhead mentioned above. Here’s how it works in 3 simple steps, from publish to syncing changes between teams.

![](https://cdn-images-1.medium.com/max/2000/0*3YCNVuGvD6MMtxz0)

## 1. Zero publish overhead

***Example:** Here’s an [example app ](https://github.com/teambit/movie-app)and [React components](https://bitsrc.io/bit/movie-app) published from it.*

Instead of forcing you to refactor your code or split repositories to publish packages, Bit lets you seamlessly **isolate** **components** in any existing project.

This is done using the bit add command to point Bit to the sets of files you want to reuse. When Bit identifies the components, it will automatically detect their dependencies so that you don’t have to configure anything.

Then, you can add pre-made build + test environments (or create your own) and lock a version for the components using bit tag. All that’s left is to publish the components using bit export and that’s it.

All the components from the project are now available to reuse and install with NPM, or to consume via bit import to develop them from any project.

This means **0 refactoring**, **0 code or file changes**, **0 configurations **and **no manual dependencies definition**. While eliminating the overhead around publishing the components, you can scale component reuse.

## 2. Discoverability and teamwork

![[https://bitsrc.io/bit/movie-app/components/hero](https://bitsrc.io/bit/movie-app/components/hero)](https://cdn-images-1.medium.com/max/2000/1*2AHGKRexNK_GP-QnjKhymA.png)*[https://bitsrc.io/bit/movie-app/components/hero](https://bitsrc.io/bit/movie-app/components/hero)*

Once exported, all the components are organized in the cloud to be made more discoverable than they would have been with NPM alone.

Here are a few key features to help find and navigate components at scale:

* **Collections**: Component can be organized into collection by theme, team, project or anything you’d like.

* **Visualization**: UI Components (React for now) can be rendered so that you can play with the components and choose with your own eyes.

* **Test + Build results**: Bit builds and runs tests for your components outside of your project, to make sure they’re battle-ready. The results are are presented as a useful indication that it works and in what cases.

* **Auto-parsed docs**: Bit will automatically parse and present docs and examples from the code itself using .md files and jsdocs.

As a result, and combined with Bit’s team managing features, you can more easily find and manage the components you share, their quality and so on.

## 3. Collaborative workflow: 2-way development

Another useful feature of Bit is that apart from installing the components with NPM/Yarn, you can also use bit import to source the components in any project using them(!).

This means that when a component needs slight improvements or modifications, you can simply import it into the consuming project, make the changes, and share the new version back out to your team’s collection.

Then, if you choose to do so and have the right permissions, you can update the component in any project it’s in, including the original repository from which it was shared.

Changes can be merged between projects using Bit’s extension of Git’s merge utility, which means the sync process across projects is smooth and fluent.

When a team or several teams are working together, this **distributed workflow** makes development and maintenance much faster over time. So far, ~75% of the thousands of developers using Bit since Jan 2018 prefer importing the components over installing them as packages.