Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Create and deploy react components with bit.dev
ClickPesa profile imageBenedict Steven
Benedict Steven forClickPesa

Posted on

     

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.

Bit.dev component driven sample app image

There are several reasons why someone might need to use Bit.dev for their React components:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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
Enter fullscreen modeExit fullscreen mode

To verify that bvm is installed, run

bvm--version
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Where:-
tasks-workspace: Workspace name
my-org: Organization / username
tasks-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
Enter fullscreen modeExit fullscreen mode

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.

bit dev server UI

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>);}
Enter fullscreen modeExit fullscreen mode

Let's create two more components, title and input to complete a form with title, inputs and a button.

run

bit create react input
Enter fullscreen modeExit fullscreen mode

and

bit create react title
Enter fullscreen modeExit fullscreen mode

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>;}
Enter fullscreen modeExit fullscreen mode

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>);}
Enter fullscreen modeExit fullscreen mode

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}/>);};
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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>);}
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

In.bitmap, we should see the new versions assigned to our new components.

To deploy the components, run

bitexport
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

After the configuration you won't need to do that again in your projects.

Then run

npm i @your-username/components.login
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
hlouzek profile image
hlouzek
  • Joined

it's good to note that if you were using an old version of bit and now want to use the new one, you must use the command "bbit" instead of "bit"

CollapseExpand
 
jinu2984 profile image
Jinay Kothari
  • Joined

ASDFGHJKL:"

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

More fromClickPesa

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp