<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mike Lu</title>
    <description>The latest articles on DEV Community by Mike Lu (@mike0120).</description>
    <link>https://dev.to/mike0120</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F885629%2Fec151f79-b87e-45fe-bb36-3f1eea86001b.png</url>
      <title>DEV Community: Mike Lu</title>
      <link>https://dev.to/mike0120</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mike0120"/>
    <language>en</language>
    <item>
      <title>How to create reusable React Components across your projects with bit.dev</title>
      <dc:creator>Mike Lu</dc:creator>
      <pubDate>Tue, 28 Nov 2023 14:49:01 +0000</pubDate>
      <link>https://dev.to/mike0120/how-to-create-reusable-react-components-across-your-projects-with-bitdev-2752</link>
      <guid>https://dev.to/mike0120/how-to-create-reusable-react-components-across-your-projects-with-bitdev-2752</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Bit.dev is a platform that makes it easy to create and share React components.&lt;/p&gt;

&lt;p&gt;There are several reasons why someone might need to use Bit.dev for their React components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reusability&lt;/strong&gt;: 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collaboration&lt;/strong&gt;: 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open-source community&lt;/strong&gt;: 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration&lt;/strong&gt;: 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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this blog, I'll explain to you the steps to create and deploy React components with Bit.dev.&lt;/p&gt;

&lt;p&gt;In order to start creating components using bit, first you need to create an account on bit.cloud.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Login to the bit.cloud, create your own scope where you will deploy your components in the dashboard.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Install bit on your machine. Follow the below commands.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install bvm (bit version manager).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g @teambit/bvm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Ensure of installed bit on your machine.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bvm --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refer to this &lt;a href="https://bit.dev/reference/reference/using-bvm/#troubleshooting"&gt;troubleshooting&lt;/a&gt; if it's not working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Init your components bit project&lt;br&gt;
Create new workspace by running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit new react tasks-workspace --default-scope my-org.tasks-scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;tasks-workspace&lt;/code&gt; means Workspace name, &lt;code&gt;my-org&lt;/code&gt; means Organization / username, and &lt;code&gt;tasks-scope&lt;/code&gt; means default scope for the new components.&lt;br&gt;
It will create a new workspace with components as a default scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Create your first component.&lt;br&gt;
You have to create your own components inside &lt;code&gt;tasks-workspace&lt;/code&gt; you just created.&lt;br&gt;
Therefore, run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd tasks-workspace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can run the following command to create the new component.&lt;br&gt;
Here, for example, let me create the &lt;code&gt;button&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit create react button
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a button component within components folder.&lt;br&gt;
The created component will have the following files&lt;/p&gt;

&lt;p&gt;&lt;code&gt;index.ts&lt;/code&gt;: Entry component file.&lt;br&gt;
&lt;code&gt;button.tsx&lt;/code&gt;: Boiler plate for creating component.&lt;br&gt;
&lt;code&gt;button.spec.tsx&lt;/code&gt;: A file to write tests.&lt;br&gt;
&lt;code&gt;button.docs.mdx&lt;/code&gt;: A documentation file.&lt;br&gt;
&lt;code&gt;button.composition.tsx&lt;/code&gt;: A file for testing different variations of the component per creator's choice.&lt;/p&gt;

&lt;p&gt;To start the development server on your local and preview the component you created, you can run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start editing the button component and preview changes on the browser.&lt;/p&gt;

&lt;p&gt;In the component folder, you can create multiple files such as stylesheet files, and components files to support the main component. For now, let's just make a button component work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { ReactNode } from 'react';

export type ButtonProps = {
  /**
   * a node to be rendered in the special component.
   */
  children?: ReactNode;
  /**
   * Onclick event
   */
  onClick?: () =&amp;gt; void
};

export function Button({ children, onClick }: ButtonProps) {
  return (
    &amp;lt;button onClick={onClick}&amp;gt;
      {children}
    &amp;lt;/button&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create two more components, title, and input to complete a form with a title, inputs, and a button.&lt;/p&gt;

&lt;p&gt;Run following 2 commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit create react input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit create react title
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On &lt;code&gt;title.tsx&lt;/code&gt; file in title folder add this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { ReactNode } from 'react';

export type TitleProps = {
  /**
   * a node to be rendered in the special component.
   */
  children?: ReactNode;
};

export function Title({ children }: TitleProps) {
  return &amp;lt;h1&amp;gt;{children}&amp;lt;/h1&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and on &lt;code&gt;input.tsx&lt;/code&gt; in input folder add this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { CSSProperties, ChangeEvent } from 'react';

export type InputProps = {
  /**
   * label text
   */
  label: string;
  /**
   * input id
   */
  id?: string;
  /**
   * input type
   */
  type?: string;
  /**
   * on input change
   */
  onChange: (value: ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt; void;
  /**
   * input value
   */
  value: string;
  /**
   * input style style
   */
  style?: CSSProperties;
};

export function Input({ label, id, type, onChange, value, style }: InputProps) {
  return (
    &amp;lt;label&amp;gt;
      &amp;lt;div&amp;gt;{label}&amp;lt;/div&amp;gt;
      &amp;lt;input
        id={id}
        style={style}
        onChange={onChange}
        value={value}
        type={type}
      /&amp;gt;
    &amp;lt;/label&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and on the &lt;code&gt;input.composition.tsx&lt;/code&gt; add the following cpde.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { Input } from './input';

export const BasicInput = () =&amp;gt; {
  const [value, setValue] = useState('');
  return (
    &amp;lt;Input
      label="Email"
      type="email"
      onChange={(e) =&amp;gt; setValue(e.target.value)}
      value={value}
    /&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test these components you created on the live preview.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Add test on the components.&lt;br&gt;
bit uses jest under the hood to run react tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Use created components within your components project before deployment.&lt;br&gt;
With bit, we can import other components within our components without deploying the library.&lt;br&gt;
Let's look at how can do it.&lt;/p&gt;

&lt;p&gt;From the above created components, let's create a form component named &lt;code&gt;login&lt;/code&gt; which will include email and password inputs, &lt;code&gt;submit&lt;/code&gt; button, and &lt;code&gt;title&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit create react login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and on &lt;code&gt;login.tsx&lt;/code&gt;, add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { Input } from '@tasks-scope/components.input';
import { Title } from '@tasks-scope/components.title';
import { Button } from '@tasks-scope/components.button';

export type LoginProps = {
  onSubmit: (data: { email: string; password: string }) =&amp;gt; void;
};

export function Login({ onSubmit }: LoginProps) {
  const [data, setData] = useState({
    email: '',
    password: '',
  });
  return (
    &amp;lt;form
      onSubmit={(e) =&amp;gt; {
        e.preventDefault();
        onSubmit(data);
      }}
    &amp;gt;
      &amp;lt;Title&amp;gt;Welcome Back&amp;lt;/Title&amp;gt;
      &amp;lt;Input
        value={data.email}
        onChange={(e) =&amp;gt;
          setData({
            ...data,
            email: e.target.value,
          })
        }
        label="Email"
      /&amp;gt;
      &amp;lt;Input
        value={data.password}
        onChange={(e) =&amp;gt;
          setData({
            ...data,
            password: e.target.value,
          })
        }
        label="Password"
      /&amp;gt;
      &amp;lt;Button&amp;gt;Submit&amp;lt;/Button&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have a working form component built with three other components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;: Install packages.&lt;br&gt;
There will be times when you will want to create a component but that component will need an external package to work, for instance, one might need a &lt;code&gt;styled-components&lt;/code&gt; package to create a component.&lt;/p&gt;

&lt;p&gt;In this case, run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit install styled-components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 8&lt;/strong&gt;: Deploy your components.&lt;br&gt;
To deploy your lib you need to login to bit using CLI.&lt;br&gt;
Run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open the browser and give you access to deploy your library. Before deploying we need to tag all your components.&lt;br&gt;
Tagging components assigns new versions to our components.&lt;/p&gt;

&lt;p&gt;Run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit tag [tag name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;.bitmap&lt;/code&gt;, we should see the new versions assigned to our new components.&lt;/p&gt;

&lt;p&gt;To deploy the components, you have to run the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit export
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will deploy your new component library.&lt;/p&gt;

&lt;p&gt;For an existing workspace when you want to edit your components or create new components, first you will need to import all deployed components by running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bit import
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And after making all changes, use the same process as we did while deploying components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9&lt;/strong&gt;: Install and use your deployed components on your react project.&lt;/p&gt;

&lt;p&gt;In your react project, install your components and start using them.&lt;/p&gt;

&lt;p&gt;the first time you are installing your components in your project, you will need to configure your bit organization or username first by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm config set '@your-username:registry' https://node-registry.bit.cloud
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the configuration you won't need to do that again in your projects.&lt;/p&gt;

&lt;p&gt;Then run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @your-username/components.login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can start using your components as you used them in the &lt;code&gt;composition&lt;/code&gt; files in your bit components.&lt;/p&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Feel free to contact me if any questions.&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>typescript</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
