Sharing React Components across Multiple Applications with BIT.dev

Componentization and re-usability are known concepts in the software engineering world. A reusable component in React is a piece of UI element that performs a specific function and can be used in various parts of a single or separate application to build more than one UI instance.ย ย 

For example, button is a UI element that can be used in various applications, and each of them handles different responsibilities. But button has lesser functionalities(attributes) to handle, like onclick, type, style, and size. Button has a few dependencies and many dependents. This relationship shows that this is a highly reusable UI component.ย ย 

We often need the same component all over the place, and at some point, we will end up writing the same code over and over in different applications. Sometimes it is ok to write or copy the same code in various projects if components are smaller, like button, but what if components are large or complex components?ย ย 

Here component sharing comes into the picture between different component-based applications developed with React and VueJs.ย ย 

Ways to share components across appsย 

  1. NPM โ€“ Code sharing registry and widely used dependency installer methodย 
  2. Bit โ€“ Component sharing through bit.cloudย 
  3. Lerna โ€“ a tool to manage Mono repos with Git and npmย 
  4. Multiple Git repositories โ€“ Reusable code in single Git repo and used in others as a packageย 

NPM and BIT / Bit. devย ย 

Bitโ€ฏis an extensible OSS tool for component-driven development. It is used to develop, source control, and collaborate onโ€ฏindividualโ€ฏcomponents.ย 

We may get confused between bit and npm, but bit is neither a package registry nor a package publishing place. Bit is an extensible component-driven development. We can develop and export the components in a remote workspace in collaboration and extend components separately. Also, we can push individual components to bit repo. At the same time, npm is a package manager and publisher based on the Javascript language. We can build standalone packages with the help of npm.ย 

In component-driven development, one advantage Bit provides, is the visibility of source code in the app where it is used. Bit also manages the versioning of the components.ย 

Letโ€™s bit. devย ย 

Bit is just like git, however, only for component-based applications. Bit is a distributed version control system for components and their dependencies.ย 

What is bit.dev?ย 

Bit. dev is a cloud-based platform for components where we can modularize and reuse with Bit. We can build small, independent components with the help of bit. Also, we can develop our service on top of Bit components.ย 

Common developers use case
Common developers use case

Fig. 1 Common developers use case
ย 

Let us understand common BIT Vocabulary before we get to the commands.ย 

  • WORKSPACE โ€“ Group of files or directories which provide context for components to be versioned, composable, and maintainedย 
  • SCOPE โ€“ This is a collaboration server for components where we can put together all componentsย 
  • STATUS – Bit workspaces track components’ changes and show each component’s state in your project’s workspace.ย 
  • NPX โ€“ is a tool to execute packages in that directory onlyย 
  • INSTALL after installation, the component resides in the node modules directory.ย 
  • FORK โ€“ copies code from remote (do not have permission to edit) workspace to your workspaceย ย 
  • IMPORT โ€“ copies code from remote (have permission to components) workspace to your workspace where you can make changes as per requirementsย 

 

Let us continue with actual commandsย 

Installing BITย 

> npx @teambit/BVM install
ย 

Initiate BIT workspaceย 

We need to initiate Bit Workspace on bit.cloud. This is a distributed component service where we can create new components, use and modify existing components, and export your changes.ย 

Run the below command for bit workspaceย 

> bit new react my-wiki –default-scope my-org.wikiย 

The above command will create react project named my-wiki with bit default scope.
But if we want to initialize an existing project with the bit then Go to that project and run the below commandย 

> bit initย 

It will provide the below outputย 

> successfully initialized bit workspace ย 

To start the server for the bit project, you can start it by using the below commandย 

> bit startย 

ORย 

> npm startย 

Page Breakย 

ย 

Create a remote scopeย 

A scope is a server where we can collaborate, export, and store components remotely.ย 

Then we must create one scope in bit.cloud account. For that, go to bit.cloud, create your account, and create scope under it.ย 

Then go to your local project directory and find worspace.jsonc file and update โ€œdefaultScope”: “organisation_name.your_scope_name”ย 

This will help while adding and exporting components from a local project to bit.cloud repositoryย 

Then choose or create components which we can use in other projects like buttons, dialog boxes, forms, cards, or navbars.ย ย 

After creating a shared(common) component, add that component to your bit.cloud by the below commandย 

Add the component to bit locallyย 

> bit add folder_path/folder_nameย ย 

After adding components on bit locally we must tag those componentsย 

> bit tag –message โ€œinitial message.โ€ย ย 

Export changes to remote scopesย 

After tagging component changes, we can export those components to remote scope on bit.cloudย 

> bit exportย 

Now exported components can be used by any developer in any other project if the access to that scope is public.ย 

Install component [NPM]ย 

If you only want to use shared components in your project and you do not want to change or maintain them, then you must install them through bit or npm by the following commandย 

> bit install @afourtech/shared_components.dialogย 

> npm install @afourtech/shared_components.dialogย 

ย 

Import component [Code sharing with the same version]ย 

The components which need to be maintained or updated for the project then we must import from remote scope into the local workspaceย 

> bit import afourtech.shared_components/dialogย ย ย 

ย 

After importing successfully, its source code has been checked into your workspace, and the package gets generated into its node_modules directory. The component is imported into your workspace and can be maintained and exported like other components.ย 

Sometimes there are cases where your imported components have used other or imported other components i.e.; it is dependent on other components, then we must include dependant components also.ย 

To achieve the above scenario, we can run the below command into the workspaceย 

ย > bit importย  – -dependentsย 

ย 

Fork Component [Code sharing with branch]ย 

For the components we want to use in our project but do not have write access for that component, we can fork that component into our project and then edit it as per need. While forking, it is copied into our workspace default scopeย 

Below is the command to fork the component from remoteย 

> bit fork afourtech.shared_components/dialogย ย 

After installing, importing, OR forking components from a remote workspace, we can just use them as it is by importing that from the actual path in needed filesย 

Like below is an exampleย 

>import Dialog from @bit/afourtech.shared_components.dialogย ย 

Developer use cases:ย 

  1. Share component uses it with NPM in another app.ย 
  2. Share a component from an App, import it into another App and see the code in both apps
    Always change the code of the shared component from one app only.ย 
  3. Share components and allow the creation of branches separately in both the appsย 

BIT sharing and securityย 

Bit proposes security through login while sending data to bit. Also, we can add the SSH key to bit.cloud for exporting components.ย 

Below is the command to log in to bit.ย 

ย > bit loginย 

This command will open bit.cloud website, where we can log in to bit.cloud account.ย 

And we are free to export and reuse components.ย ย 

Bit components that have been exported to the organisations’ scope, then all the organizations’ members can access those components in their workspace.ย ย 

ย 

References:ย 

https://bit.dev/docs/quick-startย 

https://bit.cloud/ย 

 

Authored by Sonali Barkund and Gaurish Abhisheki

Leave a Reply