<?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: Arpit Jain</title>
    <description>The latest articles on DEV Community by Arpit Jain (@arpitjain_in).</description>
    <link>https://dev.to/arpitjain_in</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%2F95093%2Fb82ee00e-49bc-4d8c-aebb-0d8b3adb3546.png</url>
      <title>DEV Community: Arpit Jain</title>
      <link>https://dev.to/arpitjain_in</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arpitjain_in"/>
    <language>en</language>
    <item>
      <title>CREATE LIBRARY OF REACT COMPONENT</title>
      <dc:creator>Arpit Jain</dc:creator>
      <pubDate>Sun, 18 Aug 2019 03:54:04 +0000</pubDate>
      <link>https://dev.to/arpitjain_in/create-library-of-react-component-1fa8</link>
      <guid>https://dev.to/arpitjain_in/create-library-of-react-component-1fa8</guid>
      <description>&lt;p&gt;&lt;code&gt;Create React App&lt;/code&gt; is a comfortable environment for learning and developing React application. It is the best way to start building a single-page application in React.&lt;/p&gt;

&lt;p&gt;By adding few more steps, we can convert our React component into the library. Later, you can publish it to NPM and let the world use that component as reusable and ready-made component into their application.&lt;/p&gt;

&lt;p&gt;SO, LET’s BEGIN THE JOURNEY!!!&lt;/p&gt;

&lt;p&gt;This journey having 9 milestones. All’re pretty simple and easily achievable.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #1&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you already have &lt;code&gt;create-react-app&lt;/code&gt; boilerplate CLI install, you can skip, otherwise have it install using below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g create-react-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once you have it successfully installed, you’re ready to create a new project using above installed boilerplate and to do that please run below command.&lt;br&gt;
create-react-app my-first-react-library&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;’my-first-react-library’ is my project name. You’re free to give any name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulation! First milestone achieved.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #2&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here, we have to perform two mini steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove all the content available inside folder &lt;code&gt;src&lt;/code&gt; and perform below inside src folder.&lt;/li&gt;
&lt;li&gt;Create a new file, give name as &lt;code&gt;index.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create two folder and give name as &lt;code&gt;lib&lt;/code&gt; and &lt;code&gt;module&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create one more index.js but this time inside &lt;code&gt;lib&lt;/code&gt; folder. It should be like &lt;code&gt;lib/index.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create one more index.js but this time inside &lt;code&gt;module&lt;/code&gt; folder. It should be like &lt;code&gt;module/index.js&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your folder hierarchy should like below: -&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0m4EEI8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ttcqnevmd2vdka0v9mhf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0m4EEI8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ttcqnevmd2vdka0v9mhf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #3&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now, whatever component’s code you want to package to make it as reusable component, need to put inside &lt;code&gt;src/lib&lt;/code&gt; folder.&lt;br&gt;
&lt;code&gt;src/lib&lt;/code&gt; folder will be considered as distributed code and entire content from this folder will be published to NPM. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Any external file like CSS or images which are the part of your component, should available inside the "src/lib" only.&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #4&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the second milestone, we’ve created &lt;code&gt;src/lib/index.js&lt;/code&gt;. Now it’s the time of using that. Here, we need to import our component reference and export it, for the use as an external package.&lt;/p&gt;

&lt;p&gt;Finally, your &lt;code&gt;lib/index.js&lt;/code&gt; should look like this: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import &amp;lt;your-component&amp;gt; from './ &amp;lt;your-component-reference&amp;gt;'
 export default &amp;lt;your-component&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you want to export single component, you can declare it as &lt;code&gt;export default&lt;/code&gt;. Or if you have multiple components to export, in that case your declaration should be like &lt;/p&gt;

&lt;p&gt;&lt;code&gt;export {your-component-1, your-component-2}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For better understanding, please refer to below GitHub location: -&lt;br&gt;
&lt;code&gt;https://github.com/arpitjain-in/create-react-component-library&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #5&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before publishing it to NPM, it is always good practice to test it a local machine and if necessary, we can debug too at local machine.&lt;br&gt;
Let’s start testing using &lt;code&gt;src/module/index.js&lt;/code&gt;. Your code snippet should look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import &amp;lt;your-component&amp;gt; from '../lib';
const App =()=&amp;gt; &amp;lt;your-component&amp;gt;;
export default App;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, for better understanding, please refer above mentioned github link.&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;module/index.js&lt;/code&gt; reference to &lt;code&gt;src/index.js&lt;/code&gt;,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';
import App from './module';
ReactDOM.render( &amp;lt;App/&amp;gt;, document.getElementById('root') );
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After following till now, You’re ready to run &lt;code&gt;npm start&lt;/code&gt; and to see the magic on browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #6&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To make component publish-able, we need to some changes in package.json as well as need to install &lt;code&gt;babel-cli&lt;/code&gt;.&lt;br&gt;
First thing first, install babel-cli using below command: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install babel-cli --save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After successful install, create &lt;code&gt;.babelrc&lt;/code&gt; file at the root level of the project and add below content: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ “presets” : [[ “react-app”,{ “absoluteRuntime”:false }]]}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In package.json, change the “build” script to the below content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build": "rm -rf dist &amp;amp;&amp;amp; NODE_ENV=production babel src/lib --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With above script changes, &lt;code&gt;src/lib&lt;/code&gt; folder code will transpile to “dist” folder.It will also ignore spec, test and snapshot specific JS files.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #7&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Perform below steps: -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delete the line "private": true from package.json. &lt;/li&gt;
&lt;li&gt;Move react-scripts, react and react-dom from dependencies to 
devDependencies. &lt;/li&gt;
&lt;li&gt;Optionally, you can also add react and react-dom to peerDependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Congratulation! All major milestones achieved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #8&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Publishing Preparation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "main": "dist/index.js",
  "module": "dist/index.js",
  "files": [ "dist", "README.md" ],
  "repository": {
    "type": "git",
    "url": "URL_OF_YOUR_REPOSITORY"
    } 
  “author”: &amp;lt;your-name&amp;gt;
  “keyword”: [&amp;lt;mention all your keywords comma separated&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;MILESTONE #9&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;And Finally, “Do Publish”&lt;/p&gt;

&lt;p&gt;For public NPM push &lt;br&gt;
&lt;code&gt;npm run publish –-access public&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For private push&lt;br&gt;
&lt;code&gt;npm run publish&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Created by &lt;a href="https://arpitjain.in"&gt;Arpit Jain&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>reactcomponent</category>
      <category>reactlibrary</category>
      <category>reusablecomponent</category>
    </item>
  </channel>
</rss>
