How to Easily Reuse Components Across Projects and Repositories
Tags: javascript, react, angular, vue
Published: 2019-09-17 15:17:07

###Build, distribute and collaborate over components to build multiple projects and applications — a practical guide.


Components [drive the development](https://itnext.io/a-guide-to-component-driven-development-cdd-1516f65d8b55) of modern applications. Apart from being UI elements that form the [experience for your users](https://blog.bitsrc.io/building-a-consistent-ui-design-system-4481fb37470f), they are also [reusable and modular code units](https://addyosmani.com/first/) which should often be used in more than one project.

When you start diving into the world of building a [shared component architecture](https://blog.bitsrc.io/sharing-components-is-caring-f8235cf1a0c) for your organization, you end up having to figure out some tough questions such as how to develop components independently but avoid the overhead of too many repositories, how to version, publish and manage each component individually, how to help others discover and adopt the components and so on.

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

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

These are deep questions, which range from the monorepo experience of component development to cross-repository management of components, and beyond. In this post, we’ll see how you can leverage useful tooling and workflows to develop, distribute and adopt components across projects.

## Framework/no framework

You basically have two choices here. The first is to use a framework like React, Angular or Vue. The advantage is that you can enjoy all the scaffolding and advantages of the well-matured framework.

But, you can also choose [web-components](https://blog.bitsrc.io/9-web-component-ui-libraries-you-should-know-in-2019-9d4476c3f103) and their [tooling like Stencil etc](https://blog.bitsrc.io/7-tools-for-developing-web-components-in-2019-1d5b7360654d). The advantage is that these components are frame-work agnostic, which helps reuse and standardize your components across different projects.

## Monorepo experience for multiple repositories?

You have to figure out which architecture is right for you. To avoid the overhead of developing each component in a standalone reposiotry, [many teams choose](https://blog.bitsrc.io/do-we-really-use-reusable-components-959a252a0a98) to group components into component libraries.

Designated tools like [Bit (GitHub)](https://github.com/teambit/bit) let you develop, build and test individual components in the library in complete isolation. Using Bit, each component in any reposiotry (and library) can be tracked as a standalone unit, and automatically packed it with all its dependencies. Then components can be independently linked to a compiler (zero configs needed(!)), to that they can be built, tested and renders in isolation. Then, components can be versioned and published individually from the repository. When you update a single components, Bit “knows” which other components depend on it, and help you update only the necessary dependency graph of the component.

It also provides additional “monorepo-like” features which are very useful for code-sharing, such as automatic local linking, multi-component configurations control, subsets and incremental builds, cyclic dependencies handling, dynamic workspaces, single NPM install and more.

Another option is to refactor all [projects to a monorepo](https://blog.bitsrc.io/sharing-components-is-caring-f8235cf1a0c), but that is a massive decision and isn’t required just to share code. You can also other great aid tools (like Lerna) to version and publish components from the repo, but most will require extensive refactoring and configurations and don’t control the dependency graph of components. Either way, try and see what works.

## Distribution and consumption across repositories

##

When you publish your components things are far from over. You have to make sure people can practically find, use and update the components.

Otherwise, you risk puting in all this work for nothing; most people won’t go through much bother to seek your components and won’t adopt components that they can’t modify if they have to. Diving into the whole library and updating it just for one component can be a cumbersome process.

## Set up a shared component portal; Make it look good!

![](https://cdn-images-1.medium.com/max/2000/0*TilCCJsC56-IB69s.gif)

_Create your component hub in [bit.dev](https://bit.dev)_

To bring everyone together on your shared components and to consolidate the discovery and consumption of components, you can create your own component portal or just use a cloud-based portal like [bit.dev](https://bit.dev/).

The key ingredients provided by such a portal are:

* Finding components shared by your team.

* Learning about their APIs and playing with live rendered examples.

* Consuming individual components to use in different projects.

* Ideally, running build and tests for every component.

If you build it on your own, it might take a while but it’s possible. If you choose [bit.dev](https://bit.dev/), you get all of the above out-of-the-box alongside features for managing permissions, controlling workflows etc. It will host your components, run their CI, let you save and play with rendered code examples, extract API docs for components and let you install them using npm/yarn.

![](https://cdn-images-1.medium.com/max/2000/0*NPpjz0Jyl-ukCOmU.gif)

_[bit.dev](https://bit.dev)- find, learn, try and install components_

## Enable a single or bulk components update; not the whole library

You can leverage Bit to version and publish individual components from a reposiotry, and even publish multiple components together as a bulk.

When you make changes to one component, Bit will let you update the version only of the component itself and the components depending on it in the reposiotry, so you don’t have to bump the whole library.

Then, the consumers of these components can get updates only for the components they install (and their dependents), without having to bring in redundant updates for the whole library.

## Enable component modifications and updates from any consuming repository; manage and control changes across projects

When you publish components to [bit.dev](https://bit.dev/) you can do more than just install them as packages; you can bit import the actual source code of the components into any consuming repository. **No context switching** and you don’t have to dive back to the publishing repo to suggest your changes.

This “cloned” version of the component comes with all the relevant files and dependencies, a linked compiler, and is versioned and managed by Bit. As a result, you can just edit the code and run the build+tests in isolation.

When you are happy with the result just bit tag a new version and export it back to bit.dev. The changes can then be updated into any other project, including the original publiching library. Changes can be merged using Git inside the repo so that you can easily sync component changes everywhere.

## Component design system for developers

When you build and share components you are really building a [UI component design system](https://codeburst.io/ui-design-system-and-component-library-where-things-break-d9c55dc6e386). This system has the power to standardize your development and consolidate a consistent and happy visual and functional experience to the users of your applications.

Only [instead of just a library](https://codeburst.io/ui-design-system-and-component-library-where-things-break-d9c55dc6e386), you get a living and breathing component ecosystem within the organization. Instead of a static docs site, you get a component hub with visually rendered components where everyone can easily find, view and even play with the actual components you build.

## Visual design system with actual code components

![](https://cdn-images-1.medium.com/max/2708/0*9enzdHDn0oXDClut.png)

_UI components design system: build, distribute, adopt_

Through bit.dev the actual code-components (React, Vue, Angular) are rendered and visualized so that both developers and designers can learn exactly how their components look, behave and feel-like to users. They can even play with the components at any moment, in an editable playground.

Developers can easily find and install the components right from the same place where they and the designers can see the visualized components.

When developers make a change and update a version for a specific component, the designers can instantly see the new version and monitor the changes to make sure the actual components fit their design system.

This creates a balance when developers have the flexibility to make changes to a component when they have to, and suggest updates from their own projects, and designers can collaborate to review changes over time.

## Component library design system workflow

Using Bit you can build, pack and publish components in isolation in the library. Then, you can export them to bit.dev from where they become a visual design system made of actual source-code components.

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

From the bit.dev design system components can be installed in other projects, or imported into them to make changes, which in turn can be synced back to the library via bit.dev. This workflow creates a few major advantages:

* **Modular component development**: Automatically encapsulate components in the repository with all their dependencies, link a compiler with 0 build configurations, and you can easily start building, testing and updating components in the repo and only their dependencies. Then, you can bulk-publish your components, each as a standalone package.

* **Simplified component discovery and consumption**: Consodilate your components into one portal from which they are discovered and consumed. Easily update and control changes across projects.

* **Increased component adoption and collaboration**: Let yourself and others on your team suggest changes to components right from their own projects, without context-switching to make a PR in your complex library. Increase the adoption and usage of components, reduce copy-pastes, improve UI standardization and consistency across app and screens.

## Conclusion

Reusing components across projects and repositories is all about creating a productive component-economy between your projects and team members. Whether you are working with a library or just sharing components directly between projects, the same 3 keys remain: Develop components as reusable units, distribute them so that people can find and use them, let yourself and others adopt the components and collaborate on changes.

Feel free to check out [Bit on GitHub](https://github.com/teambit/bit) and don’t hesitate to get in touch with any questions, comments or feedback. Happy component development! 🐥