
Create and deploy react components with bit.dev
React is one of the most popular JavaScript libraries for building user interfaces. One of the key benefits of React is its component-based architecture, which allows developers to create reusable UI components that can be shared across different projects.
Bit.dev is a platform that makes it easy to create and share React components.
There are several reasons why someone might need to use Bit.dev for their React components:
Reusability: Bit.dev allows developers to create and share reusable components across different projects and teams. This can greatly reduce development time and ensure consistency across projects.
Collaboration: Bit.dev provides tools for testing, versioning, and collaboration, making it easy for teams to work together on component development. This can help to improve code quality and reduce errors.
Scalability: Bit.dev is a distributed and scalable platform that can handle large-scale component-based development workflows. It provides a flexible and customizable workflow that can be adapted to the specific needs of different projects and teams.
Open-source community: Bit.dev has a thriving open-source community that contributes to the platform and creates new components. This can be a great resource for developers looking for inspiration or looking to contribute to open-source projects.
Integration: Bit.dev can integrate with other tools and platforms, such as Git and CI/CD pipelines, to create a seamless development workflow. This can help to streamline development processes and improve overall efficiency.
In this article, I'll walk you through the steps to create and deploy React components withBit.dev.
To start creating components using bit, first we need to create accounts onbit.cloud.
Step 1: From the bit.cloud dashboard, create a scope, a place where you will be deploying your components.
Step 2: Install bit on your machine by following this set of commands
First install bvm
npm i-g @teambit/bvm
To verify that bvm is installed, run
bvm--version
if no bvm command is not found, followthis instructions
Step 3: Initialize your components project
Create new workspace by running
bit new react tasks-workspace--default-scope my-org.tasks-scope
Where:-tasks-workspace
: Workspace namemy-org
: Organization / usernametasks-scope
: default scope for the new components.
This by default will create a new workspace with components as a default scope.
Step 4: Create your first component.
To create your first component, cdtasks-workspace
run
bit create react button
This will create a button component within components folder.
The created component will have a the following files
index.ts
: Entry component file.button.sx
: Boiler plate for creating component.button.spec.tsx
: A file to write tests.button.docs.mdx
: A documentation file.button.composition.tsx
: A file for testing different variations of the component per creator's choice.
Runbit start
to start the development server. Once the server is ready, on the browser, we should see this UI.
click on button component to open live preview of the created component.
Start editing the button component and preview changes on the browser.
In the component folder you can create multiple files such as stylesheet files, components files to support the main component. For now lets just make a button component work.
importReact,{ReactNode}from'react';exporttypeButtonProps={/** * a node to be rendered in the special component. */children?:ReactNode;/** * Onclick event */onClick?:()=>void};exportfunctionButton({children,onClick}:ButtonProps){return(<buttononClick={onClick}>{children}</button>);}
Let's create two more components, title and input to complete a form with title, inputs and a button.
run
bit create react input
and
bit create react title
Ontitle.tsx
file in title folder add this code
importReact,{ReactNode}from'react';exporttypeTitleProps={/** * a node to be rendered in the special component. */children?:ReactNode;};exportfunctionTitle({children}:TitleProps){return<h1>{children}</h1>;}
and oninput.tsx
in input folder add this code
importReact,{CSSProperties,ChangeEvent}from'react';exporttypeInputProps={/** * label text */label:string;/** * input id */id?:string;/** * input type */type?:string;/** * on input change */onChange:(value:ChangeEvent<HTMLInputElement>)=>void;/** * input value */value:string;/** * input style style */style?:CSSProperties;};exportfunctionInput({label,id,type,onChange,value,style}:InputProps){return(<label><div>{label}</div><inputid={id}style={style}onChange={onChange}value={value}type={type}/></label>);}
and on the input.composition.tsx add
importReact,{useState}from'react';import{Input}from'./input';exportconstBasicInput=()=>{const[value,setValue]=useState('');return(<Inputlabel="Email"type="email"onChange={(e)=>setValue(e.target.value)}value={value}/>);};
Update the docs and test on the live preview.
Step 5: Add test on the components.
bit uses jest under the hood to run react tests.Learn more about react testing here
Step 5: Use created components within your components project before deployment.
With bit, we can import other components within our components without deploying the library. how?
From the above created components, let's create a form component, call it login component which will include email and password inputs, submit button and title.
Run
bit create react login
and onlogin.tsx
importReact,{useState}from'react';import{Input}from'@tasks-scope/components.input';import{Title}from'@tasks-scope/components.title';import{Button}from'@tasks-scope/components.button';exporttypeLoginProps={onSubmit:(data:{email:string;password:string})=>void;};exportfunctionLogin({onSubmit}:LoginProps){const[data,setData]=useState({email:'',password:'',});return(<formonSubmit={(e)=>{e.preventDefault();onSubmit(data);}}><Title>Welcome Back</Title><Inputvalue={data.email}onChange={(e)=>setData({...data,email:e.target.value,})}label="Email"/><Inputvalue={data.password}onChange={(e)=>setData({...data,password:e.target.value,})}label="Password"/><Button>Submit</Button></form>);}
Now we have a working form component built with three other components.
Step 6: Install packages
There will be times that you will want to create a component but that component will need an external package to work, for instance one might need astyled-components
package to create a component.
Run
bitinstallstyled-components
This way one can use this package in any component they are creating
Step 7: Deploy your components
To deploy your library you need to login to bit cli,
run
bit login
This will open the browser and give you access to deploy your library.
Before deploying we need to tag all our components. Tagging components assigns new versions to our components.
run
bit tag
In.bitmap
, we should see the new versions assigned to our new components.
To deploy the components, run
bitexport
This will deploy your new component library.
To view your fresh components visithttps://bit.cloud/your-username/tasks-workspace.
For an existing workspace when you want to edit your components or creating new components, first you will need to import all deployed components by running
bit import
And after making all changes, use the same process as we did while deploying components.
Step 8: Install and use your deployed components on your react project.
In your react project, install your components and start using them.
the first time you are installing your components in your project, you will need to configure your bit organization or username first by running
npm configset'@your-username:registry' https://node.bit.cloud
After the configuration you won't need to do that again in your projects.
Then run
npm i @your-username/components.login
Then you can start using your components as you used them in thecomposition
files in your bit components.
Congratulations!!
Creating and deploying React components with Bit.dev is a powerful way to streamline your development workflow and make your components reusable across different projects. By following the steps outlined in this article, you can easily create, test, and share React components with other developers using the Bit.dev platform.
If you like this article, there are more like this in our blogs, follow us ondev.to/clickpesa,medium.com/clickpesa-engineering-blog andclickpesa.hashnode.dev
Happy Hacking!!
Top comments(2)
For further actions, you may consider blocking this person and/orreporting abuse