<?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: Sanskar Gupta</title>
    <description>The latest articles on DEV Community by Sanskar Gupta (@sanskar95).</description>
    <link>https://dev.to/sanskar95</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%2F424959%2F4766482d-f530-4350-a5f0-b39753c7ee3e.jpeg</url>
      <title>DEV Community: Sanskar Gupta</title>
      <link>https://dev.to/sanskar95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanskar95"/>
    <language>en</language>
    <item>
      <title>Ag-grid hacks 1 - getting updated rows</title>
      <dc:creator>Sanskar Gupta</dc:creator>
      <pubDate>Sun, 02 Aug 2020 09:54:46 +0000</pubDate>
      <link>https://dev.to/sanskar95/ag-grid-hacks-1-getting-recently-updated-rows-4bh5</link>
      <guid>https://dev.to/sanskar95/ag-grid-hacks-1-getting-recently-updated-rows-4bh5</guid>
      <description>&lt;h3&gt;
  
  
  Prologue
&lt;/h3&gt;

&lt;p&gt;Implementing ag-grid functionalities can be quite frustrating at times especially when you are handling the enterprise version, the ag-grid hacks series will be considerate in reducing implementation time and reducing the extent of frustration that may arise scavenging the internet for sample implementations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;To all who are not familiar: Ag-grid is a library to implement data grids and charts in your project , where data changes can be in real time as well, the community version is free while you need to pay a hefty amount to get the enterprise version which is loaded with more features.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem Statement
&lt;/h3&gt;

&lt;p&gt;It becomes difficult at times to get the list of all recent rows in the from of objects while dealing with an editable ag-grid as there is no straight api like getUpdatedRows() in ag grid documentation though we can trigger a method when cell value changes.&lt;br&gt;
&lt;a href="https://www.ag-grid.com/javascript-grid-change-detection/" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;Consider the following piece of code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AgGridReact&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ag-grid-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ag-grid-community/dist/styles/ag-grid.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ag-grid-community/dist/styles/ag-theme-balham.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getColumnDefs&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./ColumnDefUtils&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;columnDefs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;headerName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;City&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;editable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;headerName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Population&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;editable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},{&lt;/span&gt;
      &lt;span class="na"&gt;headerName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Currency&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;children&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;editable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

&lt;span class="p"&gt;]&lt;/span&gt;



&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;rowData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;methodToFetchDataFromBackend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;rowData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;getUpdatedRows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rowData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;changed&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;handleSave&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// filter the row data(this.state.rowData) where the changed in true and send ony those objects to backend&lt;/span&gt;
    &lt;span class="nf"&gt;saveMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUpdatedRows&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;onCellValueChanged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;saveButtonDisableFlag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;changed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AgGridReact&lt;/span&gt;
        &lt;span class="nx"&gt;columnDefs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;columnDefs&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;rowData&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rowData&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onCellValueChanged&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onCellValueChanged&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Consider the rowData being updated when the component mounts by fetching data from backend. Ag grid api provides an inbuilt method "onCellValueChanged" that is triggered when we edit the grid.&lt;br&gt;
Ag grid api methods: &lt;a href="https://www.ag-grid.com/javascript-grid-api/" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The line &lt;code&gt;params.node.data['changed'] = true&lt;/code&gt; inserts an extra field to the row object whose cell underwent change.&lt;br&gt;
Note that, here params contains all information about the row and cell.&lt;br&gt;
Here is the sample params object&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhvnrrdi1isgv4flrn5r7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhvnrrdi1isgv4flrn5r7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It contains the oldValue , newValue , the data is the rowData for which the cell belongs.&lt;br&gt;
In the above code getUpdatedRows filters the data by "changed " field and sends only the updated rows to the backend for saving.&lt;/p&gt;

&lt;p&gt;This is how one can get hold of updated rows by a simple hack!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Add a rich text editor to your app:  react-draft-wysiwyg</title>
      <dc:creator>Sanskar Gupta</dc:creator>
      <pubDate>Wed, 22 Jul 2020 19:35:08 +0000</pubDate>
      <link>https://dev.to/sanskar95/add-a-rich-text-editor-to-your-app-react-draft-wysiwyg-44ob</link>
      <guid>https://dev.to/sanskar95/add-a-rich-text-editor-to-your-app-react-draft-wysiwyg-44ob</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The npm library react-draft-wysiwyg is one of the popular ready to use rich text editor framework built on top of react and draft-js which is in turn a framework for building rich text editors that leverages the power of immutable collections in javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Rich text and plain text&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Rich Text Format (RTF) is a file format that allows the exchange of text files between different editors. ... Plain Text: Plain text contains no formatting, only line breaks and spacing. Therefore no text formatting (such as font sizes and colors, bolding or italics) can be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Use case&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A rich text editor has many use cases, some of them being creating website wide announcements, illustrative and emphasized description of products.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Installation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Execute &lt;code&gt;npm install draft-js he react-draft-wysiwyg&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that he is an html encoder/decoder.It will be used to decode text coming from the backend and covert to object.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;An example&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The best way to get acquainted with a concept is by an example , so let's follow this approach.&lt;br&gt;
The following example will demonstrate step by step process to show, edit and save text in the text editor.&lt;/p&gt;

&lt;p&gt;The message coming form backend will be in the form of an unescaped html string eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{&amp;amp;#34;blocks&amp;amp;#34;:[{&amp;amp;#34;key&amp;amp;#34;:&amp;amp;#34;13trq&amp;amp;#34;,&amp;amp;#34;text&amp;amp;#34;:&amp;amp;#34;Important message!&amp;amp;#34;,&amp;amp;#34;type&amp;amp;#34;:&amp;amp;#34;unstyled&amp;amp;#34;,&amp;amp;#34;depth&amp;amp;#34;:0,&amp;amp;#34;inlineStyleRanges&amp;amp;#34;:[],&amp;amp;#34;entityRanges&amp;amp;#34;:[],&amp;amp;#34;data&amp;amp;#34;:{}}],&amp;amp;#34;entityMap&amp;amp;#34;:{}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that in the above sample response the text that will be shown is "Important message!"&lt;/p&gt;

&lt;p&gt;Lets decode this html string using he and set the editor state using below sample function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Editor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-draft-wysiwyg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;EditorState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;convertFromRaw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;convertToRaw&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;draft-js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-draft-wysiwyg/dist/react-draft-wysiwyg.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;he&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;he&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;//some_code&lt;/span&gt;


&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;setUpEditor&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serverResposne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{&amp;amp;#34;blocks&amp;amp;#34;:[{&amp;amp;#34;key&amp;amp;#34;:&amp;amp;#34;13trq&amp;amp;#34;,&amp;amp;#34;text&amp;amp;#34;:&amp;amp;#34;Important message!&amp;amp;#34;,&amp;amp;#34;type&amp;amp;#34;:&amp;amp;#34;unstyled&amp;amp;#34;,&amp;amp;#34;depth&amp;amp;#34;:0,&amp;amp;#34;inlineStyleRanges&amp;amp;#34;:[],&amp;amp;#34;entityRanges&amp;amp;#34;:[],&amp;amp;#34;data&amp;amp;#34;:{}}],&amp;amp;#34;entityMap&amp;amp;#34;:{}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;he&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;serverResposne&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;blocks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//this condition indicates empty message&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;contentState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;convertFromRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;editorState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                  &lt;span class="nx"&gt;EditorState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createWithContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;contentState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;setEditorState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;editorState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//setting the message state in editor&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that editorState is the top level object for the editor and has the following information:&lt;/p&gt;

&lt;p&gt;1)The current text content state&lt;br&gt;
2)The current selection state&lt;br&gt;
3)The fully decorated representation of the contents&lt;br&gt;
4)Undo/redo stacks&lt;br&gt;
5)The most recent type of change made to the contents&lt;/p&gt;

&lt;p&gt;More information on editorState:  &lt;a href="https://draftjs.org/docs/api-reference-editor-state/" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you start typing the editorState will get updated accordingly&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Action Buttons&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can add some action buttons beneath the editor , such as &lt;br&gt;
save, clear&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkd91ktkd4jwhokt80837.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkd91ktkd4jwhokt80837.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Clear
&lt;/h4&gt;

&lt;p&gt;This will clear out the text in the editor on button click.Here is how the onClick handler will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onClear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setEditorState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;EditorState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createEmpty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Save
&lt;/h4&gt;

&lt;p&gt;Convert the state to unescaped html string and call the save method responsible for triggering rest call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onSave&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;convertToRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;editorState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCurrentContent&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//api call here with newMessage as request body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we can check if the editor state has empty message by &lt;code&gt;editorState.getCurrentContent().hasText()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Refer &lt;a href="https://github.com/Sanskar95/react-site-announcement/blob/master/src/SiteAnnouncement.js" rel="noopener noreferrer"&gt;Example file&lt;/a&gt; for sample implementation.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>opensource</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Demystifying React Portals-Don't let the parent enforce styles on the child</title>
      <dc:creator>Sanskar Gupta</dc:creator>
      <pubDate>Sat, 18 Jul 2020 16:46:00 +0000</pubDate>
      <link>https://dev.to/sanskar95/demystifying-react-portals-don-t-let-the-parent-enforce-styles-on-the-child-1p4p</link>
      <guid>https://dev.to/sanskar95/demystifying-react-portals-don-t-let-the-parent-enforce-styles-on-the-child-1p4p</guid>
      <description>&lt;p&gt;A portal is defined as a doorway, gate, or other entrance, especially an imposing and a secretive one.&lt;/p&gt;

&lt;p&gt;This same concept is applied to a react component who is able to travel through a portal and is able to render itself on a DOM node which does NOT belong to its parent's DOM hierarchy.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why to use it?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Every UI developer might have faced a scenario where parent css styles gets applied to the children components and this takes up huge amounts of time to come up with a workaround.&lt;br&gt;
React portals enables the children not to get rendered in same DOM tree as its parent ,hence they are saved from getting css being applied to them forcefully.&lt;/p&gt;

&lt;p&gt;Some of the use cases of portals include modals, tooltips, notification bars, toasters etc.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Example use case&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Consider the following piece of code&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 "./styles.css";
import ReactDOM from 'react-dom'

const modalRoot = document.getElementById('root')
const modalRoot = document.getElementById('another_root')

class App extends React.Component {
  state = {
    show: false
  };
  showModal = e =&amp;gt; {
    this.setState({
      show: !this.state.show
    });
  };
  render() {
    return (
      &amp;lt;div style={{textAlign: 'center', maxWidth: '50%'}}&amp;gt;
        &amp;lt;button
          onClick={e =&amp;gt; {
            this.showModal(e);
          }}
        &amp;gt;
          {" "}
          show Modal{" "}
        &amp;lt;/button&amp;gt;

        &amp;lt;Modal onClose={this.showModal} show={this.state.show}&amp;gt;
          Text
        &amp;lt;/Modal&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

class Modal extends React.Component {
    onClose = e =&amp;gt; {
        this.props.onClose &amp;amp;&amp;amp; this.props.onClose(e);
    };
    render() {
        if (!this.props.show) {
            return null;
        }
        return (
            &amp;lt;div &amp;gt;
                &amp;lt;h2&amp;gt;Modal Window&amp;lt;/h2&amp;gt;
                &amp;lt;div &amp;gt;{this.props.children}&amp;lt;/div&amp;gt;
                &amp;lt;div &amp;gt;
                    &amp;lt;button  onClick={this.onClose}&amp;gt;
                        close
                    &amp;lt;/button&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }
}


ReactDOM.render(&amp;lt;App /&amp;gt;, rootElement);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here it can be seen that the parent(App) has a div whose max width is set to 50 percent , as a result this width restriction gets applied to modal as well.&lt;/p&gt;

&lt;p&gt;Here is the result:(Click on image to get the width idea)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n8-oPS3s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sybtbss3t6i4q8px3007.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n8-oPS3s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sybtbss3t6i4q8px3007.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now consider this piece of code with portals:&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 "./styles.css";
import ReactDOM from 'react-dom'

const modalRoot = document.getElementById('root')
const modalRoot = document.getElementById('another_root')

class App extends React.Component {
  state = {
    show: false
  };
  showModal = e =&amp;gt; {
    this.setState({
      show: !this.state.show
    });
  };
  render() {
    return (
      &amp;lt;div style={{textAlign: 'center', maxWidth: '50%'}}&amp;gt;
        &amp;lt;button
          onClick={e =&amp;gt; {
            this.showModal(e);
          }}
        &amp;gt;
          {" "}
          show Modal{" "}
        &amp;lt;/button&amp;gt;

        &amp;lt;Modal onClose={this.showModal} show={this.state.show}&amp;gt;
          Text
        &amp;lt;/Modal&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

class Modal extends React.Component {
    onClose = e =&amp;gt; {
        this.props.onClose &amp;amp;&amp;amp; this.props.onClose(e);
    };
    render() {
        if (!this.props.show) {
            return null;
        }
        return ReactDOM.createPortal (
            &amp;lt;div &amp;gt;
                &amp;lt;h2&amp;gt;Modal Window&amp;lt;/h2&amp;gt;
                &amp;lt;div &amp;gt;{this.props.children}&amp;lt;/div&amp;gt;
                &amp;lt;div &amp;gt;
                    &amp;lt;button  onClick={this.onClose}&amp;gt;
                        close
                    &amp;lt;/button&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;, modalRoot
        );
    }
}


ReactDOM.render(&amp;lt;App /&amp;gt;, rootElement);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is the result:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j_WxEGsb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hs719nrgxwudmv19pydt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j_WxEGsb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hs719nrgxwudmv19pydt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It can be seen that the modal is encompassing the whole width and not just the 50 percent as defined in parent.&lt;br&gt;
ReactDOM.createPortal accepts two params , the jsx and the DOM node that is present outside like the div with id and "another_root"&lt;/p&gt;

&lt;p&gt;On exploring the console:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e17pHdu2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b8dgojy2mzsh357qii08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e17pHdu2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b8dgojy2mzsh357qii08.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is proved that the modal is rendered under the "another_root" div and not the "root" on which the parent (App) is rendered.&lt;/p&gt;

&lt;p&gt;The above example demonstrates the usage of a react portal considering modal as an example.&lt;/p&gt;

&lt;p&gt;PS: It should be noted , though the child(modal) belongs to different DOM tree, there are no changes in flow of props and data i.e consider it as a normal child while dealing with props.&lt;/p&gt;

&lt;p&gt;The above code can be found at: &lt;a href="https://github.com/Sanskar95/react_portals_example"&gt;Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>uiweekly</category>
    </item>
    <item>
      <title>NPM Primer: Publishing your first npm package</title>
      <dc:creator>Sanskar Gupta</dc:creator>
      <pubDate>Wed, 15 Jul 2020 16:36:46 +0000</pubDate>
      <link>https://dev.to/sanskar95/npm-primer-publishing-your-first-npm-package-1ma7</link>
      <guid>https://dev.to/sanskar95/npm-primer-publishing-your-first-npm-package-1ma7</guid>
      <description>&lt;p&gt;Publishing an npm package to npmjs registry can be your first step towards being part of open source community.Before diving into publishing a piece of javascript code as a package, let's brush up some relevant nomenclature.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is NPM for?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;npm is a package manager for the Node JavaScript platform.It manages packages/libraries in in a single place and facilitates node to find them when required.The registry &lt;a href="http://www.npmjs.com"&gt;www.npmjs.com&lt;/a&gt; hosts numerous free packages to download ,install and use in your projects.&lt;/p&gt;

&lt;p&gt;To set up node and npm please follow the official documentation &lt;a href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Setting up the piece of code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let's consider an example and create an npm module exposing some methods that will come in handy for array processing.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Make project directory&lt;/strong&gt;
&lt;/h3&gt;



&lt;p&gt;&lt;code&gt;mkdir array-util&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;cd array-util&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Make it a node project&lt;/strong&gt;
&lt;/h3&gt;



&lt;p&gt;&lt;code&gt;npm init&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will create a file package.json that holds name of dependencies and scripts like test, eject, build etc.&lt;br&gt;
Use the command line to follow through the entire process like the below image:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CVIcWGVq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cwjb5zpzi8qzv2yqnlxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CVIcWGVq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cwjb5zpzi8qzv2yqnlxx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The package.json file will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "arrayutil",
  "version": "1.0.0",
  "description": "Package having methods for array processing",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "author": "Sanskar Gupta",
  "license": "ISC"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a directory src and place your js file and name it as utils.js .This file will hold &lt;br&gt;
all the methods that are supposed to be exposed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayutils = {
  sort: (array) =&amp;gt; {
     return array.sort()
  }
}

export default arrayutils
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that here ES6 is being used, we will convert it to ES5 using babel.&lt;/p&gt;

&lt;p&gt;Create a file export.js under src that will will serve as the entry point for the module.&lt;br&gt;
&lt;code&gt;export { arrayutils } from "./utils"&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Cant ignore .npmignore&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Create a file namely .npmignore in the project root, here we will add all the directories or files that should not contribute to out npm module for eg, .vscode, .idea , src.&lt;/p&gt;

&lt;p&gt;Note that here we are writing our module in ES6 that will be converted to ES5 later under the dist folder that will hold the cross browser supported module, as a result we need to add src (in ES6) to .npmignore to mitigate the unecessary increment in package size.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Modify package.json&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Change the main field in package.json to "./dist/export.js"&lt;br&gt;
Add a publish script under scripts&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Include babel core and babel cli&lt;/strong&gt;&lt;br&gt;
PS: Babel is a free and open-source JavaScript transcompiler that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript that can be run by older JavaScript engines.&lt;/p&gt;

&lt;p&gt;The package.json should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "arrayutil",
  "version": "1.0.0",
  "description": "Package having methods for array processing",
  "main": "./dist/export.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
    "publish:npm": "rm -rf dist &amp;amp;&amp;amp; mkdir dist &amp;amp;&amp;amp;  babel src -d dist --copy-files"

  },
"babel": {
    "presets": [
      "@babel/preset-react"
    ]
  },
  "author": "Sanskar Gupta",
  "license": "ISC"
"dependencies": {
    "@babel/core": "^7.9.0",
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/preset-react": "^7.9.1"
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that on running the command:&lt;br&gt;
&lt;code&gt;npm run publish:npm&lt;/code&gt; a folder named dist will be created having code in ES5 format.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Lets publish to npm!&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;1)Commit and push your changes to your preferred branch.We need to keep the commit tree clean before publishing to npm.&lt;/p&gt;

&lt;p&gt;2)Run &lt;code&gt;npm login&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3)Login with the credentials.Make sure you have created an account on &lt;a href="https://www.npmjs.com/"&gt;https://www.npmjs.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4)You can check your login status by &lt;code&gt;npm whoami&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;5)Run &lt;code&gt;npm run publish:npm&lt;/code&gt; to build&lt;/p&gt;

&lt;p&gt;6)If publishing for the first time you can directly run &lt;code&gt;npm &lt;br&gt;
  publish&lt;/code&gt;, if updating the current published version execute &lt;br&gt;
  &lt;code&gt;npm version &amp;lt;version_number&amp;gt;&lt;/code&gt; before publishing&lt;br&gt;
  You can follow any version sequence as per your choice &lt;br&gt;
  though it is suggested to use 1.x.x&lt;/p&gt;

&lt;p&gt;7)Go to npmjs and validate your package by downloading and using in some other project&lt;/p&gt;

&lt;p&gt;8)Note that its always good to have a readme, so make sure that README.md is added stating instructions on how to import and use the module preferably with an example.&lt;/p&gt;

&lt;p&gt;Example Repo: &lt;a href="https://github.com/Sanskar95/snack-toast"&gt;https://github.com/Sanskar95/snack-toast&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PS: You can check the download stats of your module on&lt;br&gt;
&lt;a href="https://npm-stat.com/"&gt;https://npm-stat.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>npm</category>
      <category>javascript</category>
      <category>node</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
