<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hemant Singh Nagarkoti on Medium]]></title>
        <description><![CDATA[Stories by Hemant Singh Nagarkoti on Medium]]></description>
        <link>https://medium.com/@hemantnagarkoti?source=rss-2049549d44ed------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wxo9haS6RikTHzxr.</url>
            <title>Stories by Hemant Singh Nagarkoti on Medium</title>
            <link>https://medium.com/@hemantnagarkoti?source=rss-2049549d44ed------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 16:14:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hemantnagarkoti/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Complete react boilerplate and adding custom libraries]]></title>
            <link>https://medium.com/@hemantnagarkoti/a-complete-react-boilerplate-and-adding-custom-libraries-9d92d677f2b4?source=rss-2049549d44ed------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d92d677f2b4</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Hemant Singh Nagarkoti]]></dc:creator>
            <pubDate>Sun, 26 May 2019 13:38:17 GMT</pubDate>
            <atom:updated>2019-09-17T09:30:52.657Z</atom:updated>
            <content:encoded><![CDATA[<h4>From Ground to Sky</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wyxuq21keffc5b0d_lMkUw.jpeg" /></figure><p>I started learning react, redux few years back and it was overwhelming concept/technology to start, with lot of learning new items like webpack, babel, redux, flux. #*ENGINEERING</p><p>Today the simplest way to create a react application is usingcreate-react-app with no build configurations, which help us to create a simple project but not a satisfying directory which we actually want to create with some common dependencies such as react, react-redux, react-router-dom etc.</p><p>From this it strikes in my mind why not to create a boilerplate with all the dependencies that i always use, it can save a lot of time of development and setting it up.</p><p>In this article we will learn how to create a react boilerplate from scratch with the main dependencies used in React community.</p><h3><strong>Getting Started</strong></h3><p>First things first let’s create a new folder, cd into it and start it. You can name it whatever you want to be, let’s name it react-boilerplate</p><pre>mkdir react-boilerplate</pre><p>And now create package.json file by running below command</p><pre>npm init -y</pre><p>NPM will create a package.json</p><p>Now it’s time to add dependencies to our package.json file, add below dependencies to your package.json It will look something likes this:</p><pre>&quot;dependencies&quot;: {<br>   &quot;colors&quot;: &quot;*&quot;,<br>   &quot;readline-sync&quot;: &quot;*&quot;,<br>   &quot;shelljs&quot;: &quot;*&quot;<br>}</pre><p>Now add below lines into your package.json file.</p><pre>&quot;bin&quot;: {<br>   &quot;react-redux-boiler-setup&quot;: &quot;index.js&quot;<br>}</pre><p>Now lets understand what is this.</p><p>react-redux-boiler-setup will act as a NPM module, and we commanding this NPM module to run our file named as index.js .</p><p>Now create index.js in your root directory of project.</p><p>mkdir index.js</p><p>Now we will start writing our index file and see which lines means what:</p><pre>#!/usr/bin/env node<br>let shell = require(&#39;shelljs&#39;);<br>let colors = require(&#39;colors&#39;);<br>let readlineSync = require(&#39;readline-sync&#39;);<br>let fs = require(&#39;fs&#39;);</pre><p>Now we will understand what first line ie.</p><p>#!/usr/bin/env node this is an instance of shebang line. It makes your plain javascript file into executable file in UNIX platforms.</p><p>We have imported shelljs let shell = require(&#39;shelljs&#39;); so that we can run some of our custom commands using shelljs, which we excute in our terminal.</p><p>colors package we have imported because it will allow us to see our logs in color full manners.</p><p>readlin-sync is used to read inputs through command line from user in synchronous manner.</p><p>fs is imported because we want to create some custom files, using our boilerplate.</p><p>Now add below lines to your index.js file.</p><pre>let templates = require(&#39;./templates/templates.js&#39;)<br>let appName = process.argv[2];</pre><p>templates file we will create in next few minutes.</p><p>Here we are taking input from command prompt at second index. Let’s add few next few more lines to our code.</p><pre>const run = async () =&gt; {</pre><pre>let success = await createReactApp()</pre><pre>if(!success){</pre><pre>  console.log(&#39;Something went wrong while trying to create a new     React app using create-react-app&#39;.red)</pre><pre>  return false;</pre><pre>}</pre><pre>  await takeUserInput()</pre><pre>  await cdIntoNewApp()</pre><pre>  await installPackages()</pre><pre>  await updateTemplates()</pre><pre>  console.log(&quot;All done&quot;)</pre><pre>}</pre><p>We have created a run function, and in second line createReactApp will create a new react app, we have used <a href="https://javascript.info/async-await">Javascript await</a> function.</p><p>await takeUserInput will help us to take input from the user via command line.</p><p>await installPackages() we will be installing dependencies to our application.</p><p>await updateTemplates() we will be using for writing some of jsx components to your newly created app using boilerplate.</p><p>Let’s dive more deeper by writing these functions and your index file should look like this:</p><pre>#!/usr/bin/env node</pre><pre>let shell = require(&#39;shelljs&#39;);<br>let colors = require(&#39;colors&#39;); // to add colorfull console logs<br>let readlineSync = require(&#39;readline-sync&#39;);// used to take user input<br>let fs = require(&#39;fs&#39;); //fs already comes included with node.<br>let templates = require(&#39;./templates/templates.js&#39;);</pre><pre>let appName = process.argv[2];<br>let appDirectory = `${process.cwd()}/${appName}`;</pre><pre>const dependeciesRequired = {<br>    &#39;react-materialize&#39;: {<br>         value: null,<br>         dependency: [&#39;react-materialize&#39;],<br>         dependencyType: &#39;--save&#39;<br>     },<br>    &#39;styled-components&#39;: {<br>         value: null,<br>         dependency: [&#39;styled-components&#39;],<br>         dependencyType: &#39;--save&#39;<br>     }<br>}</pre><pre>let installablePackages = [[],[]];<br>const createReactApp = () =&gt; {<br>   return new Promise(resolve=&gt;{<br>      if(appName){<br>         shell.exec(`create-react-app ${appName}`, () =&gt;{<br>           console.log(&quot;Created react app&quot;)<br>           resolve(true)<br>         })<br>      } else {<br>        console.log(&quot;\nNo app name was provided.&quot;.red);<br>        console.log(&quot;\nProvide an app name in the following format: &quot;);<br>        console.log(&quot;\nreact-redux-boiler-setup &quot;, &quot;app-name\n&quot;.cyan);<br>        resolve(false)<br>      }<br>    })<br>}</pre><pre>const run = async () =&gt; {<br>    let success = await createReactApp()<br>    if(!success){<br>        console.log(&#39;Something went wrong while trying to create a new React app using create-react-app&#39;.red)<br>        return false;<br>    }<br>    await takeUserInput()<br>    await cdIntoNewApp()<br>    await installPackages()<br>    await updateTemplates()<br>    console.log(&quot;All done&quot;)<br>}</pre><pre>const takeUserInput = () =&gt; {<br>    return new Promise((resolve, reject) =&gt; {<br>        let promises = [];<br>        Object.keys(dependeciesRequired).forEach((dependency, i) =&gt;{<br>            promises[i] = new Promise(res =&gt; {<br>            return takeInput(`Would you like to install ${dependency}? yes/no  `, res, dependency)<br>        });<br>    });<br>    Promise.all(promises).then(()=&gt;{resolve()})<br>  });<br>}</pre><pre>const takeInput = (msg, res, dependency) =&gt; {<br>    let answer = readlineSync.question(msg, {trueValue: [&#39;yes&#39;, &#39;y&#39;, &#39;Yes&#39;, &#39;YES&#39;, &#39;Y&#39;, &#39;&#39;],falseValue: [&#39;no&#39;, &#39;No&#39;, &#39;NO&#39;, &#39;N&#39;, &#39;n&#39;]});<br>    if (answer === true) {<br>        dependeciesRequired[dependency][&#39;value&#39;] = true;<br>        res()<br>    } else if (answer === false) {<br>        dependeciesRequired[dependency][&#39;value&#39;] = false;<br>        res()<br>    } else {<br>        console.log(&#39;Please enter valid input. &quot;&#39; + answer + &#39;&quot; what does it mean?&#39;);<br>        return takeInput(msg, res, dependency);<br>    }<br>}</pre><pre>const cdIntoNewApp = () =&gt; {<br>    return new Promise(resolve=&gt;{<br>        shell.exec(`cd ${appName}`, ()=&gt;{resolve()})<br>    })<br>}</pre><pre>const installPackages = () =&gt; {<br>    return new Promise(resolve=&gt;{<br>        Object.keys(dependeciesRequired).forEach((packageName, i) =&gt;{<br>        if(dependeciesRequired[packageName][&#39;value&#39;]){<br>            if(dependeciesRequired[packageName][&#39;dependencyType&#39;].includes(&#39;--save-dev&#39;)){<br>                for(let j=0; j&lt;dependeciesRequired[packageName][&#39;dependency&#39;].length; j++){<br>        installablePackages[0].push(dependeciesRequired[packageName][&#39;dependency&#39;][j])</pre><pre>       }<br>       } else {<br>          for(let j=0; j&lt;dependeciesRequired[packageName][&#39;dependency&#39;].length; j++){<br>           installablePackages[1].push(dependeciesRequired[packageName][&#39;dependency&#39;][j])<br>          }<br>         }<br>       }<br>  })<br>     console.log(`\nInstalling redux, react-router, react-router-dom, react-redux, and redux-thunk ${installablePackages[1].join(&#39; &#39;)}\n`.cyan)<br>    shell.exec(`npm install --save redux react-router react-redux redux-thunk ${installablePackages[1].join(&#39; &#39;)}`, () =&gt; {</pre><pre>    if(installablePackages[0].length){<br>         shell.exec(`npm install --save-dev ${installablePackages[0].join(&#39; &#39;)}`, () =&gt; {<br>         console.log(&quot;\nFinished installing packages installablePackages\n&quot;.green)<br>         resolve()<br>     })<br>    } else {<br>        console.log(&quot;\nFinished installing packages\n&quot;.green)<br>        resolve()<br>     }<br>   })<br>   })<br>}</pre><pre>const updateTemplates = () =&gt; {<br>    return new Promise(resolve=&gt;{<br>    let promises = [];<br>    Object.keys(templates).forEach((fileName, i)=&gt;{<br>      promises[i] = new Promise(res=&gt;{<br>      if(fileName.includes(&#39;package&#39;)){<br>        if(installablePackages[1].length){<br>          for(let i=0; i&lt; installablePackages[1].length; i++){<br>            let name = installablePackages[1][i];<br>            templates[fileName][&#39;dependencies&#39;][name]= &#39;@latest&#39;;<br>          }<br>        }<br>     if(installablePackages[0].length){<br>        for(let i=0; i&lt; installablePackages[0].length; i++){<br>          let name = installablePackages[0][i];<br>          templates[fileName][&#39;devDependencies&#39;][name]= &#39;@latest&#39;;<br>        }<br>     }<br>    }<br>    if(fileName &amp;&amp; fileName.includes(&#39;exe_comand&#39;)){<br>    shell.exec(`rm -rf ${appDirectory}/src/${templates[fileName]}`, () =&gt; {})<br>    } else {<br>    if(fileName &amp;&amp; fileName.includes(&#39;/&#39;)){<br>      let dirArray = fileName.split(&#39;/&#39;);<br>      dirArray.map((dir, index) =&gt; {<br>        return new Promise(res1 =&gt; {<br>         if(!dir.endsWith(&#39;.js&#39;)){<br>           shell.exec(`mkdir ${appDirectory}/src/${dir}`);<br>         }<br>        })<br>      })<br>    fs.writeFile(`${appDirectory}/src/${dirArray.join(&#39;/&#39;)}`,    templates[fileName], function(err) {<br>    if(err) { return console.log(err) }<br>    res()<br>     })<br>   } else {<br>    if(fileName.includes(&#39;eslint&#39;) || fileName.includes(&#39;package&#39;)){<br>      if(dependeciesRequired[&#39;eslint&#39;] &amp;&amp; dependeciesRequired[&#39;eslint&#39;][&#39;value&#39;]){<br>      fs.writeFile(`${appDirectory}/${fileName}`, JSON.stringify(templates[fileName]), function(err) {<br>      if(err) { return console.log(err) }<br>        res()<br>      })<br>    }<br>} else {<br>       fs.writeFile(`${appDirectory}/src/${fileName}`, templates[fileName], function(err) {<br>           if(err) { return console.log(err) }<br>           res()<br>        })<br>       }<br>      }<br>     }<br>   })<br>  })<br>  Promise.all(promises).then(()=&gt;{resolve()})<br> })<br>}<br>run()</pre><p>Awesome we are almost done now just need to write few more files to complete your boilerplate. Lets create a new folder named as templates and cd into it.</p><pre>mkdir templates &amp;&amp; cd templates</pre><p>Now create templates.js using totouch templates.js and write paste below code:</p><pre>module.exports = {  <br>    &#39;index.js&#39;: require(&#39;./index.js&#39;),  <br>    &#39;store/index.js&#39;: require(&#39;./store.js&#39;),  <br>    &#39;actions/index.js&#39;: require(&#39;./actions.js&#39;),  <br>    &#39;reducers/index.js&#39;: require(&#39;./reducers.js&#39;),  <br>    &#39;container/App.js&#39;: require(&#39;./App.js&#39;),  <br>    &#39;components/Header.js&#39;: require(&#39;./Header.js&#39;),        &#39;components/Footer.js&#39;: require(&#39;./Footer.js&#39;),           &#39;components/MyApp.js&#39;: require(&#39;./MyApp.js&#39;),  <br>    &#39;exe_comand&#39;: &#39;App.js&#39;,  <br>    &#39;exe_comand1&#39;: &#39;App.css&#39;,  <br>    &#39;exe_comand2&#39;: &#39;App.test.js&#39;,  <br>    &#39;exe_comand3&#39;: &#39;index.css&#39;,  <br>    &#39;exe_comand4&#39;: &#39;logo.svg&#39;,  <br>    &#39;exe_comand5&#39;: &#39;serviceWorker.js&#39;<br>}</pre><p>and we will create all these files into our templates directory.</p><blockquote>Remember exe_comand and exe_command1 etc mapped for deleting directories created by create-react-app</blockquote><p>Let create each file one by one.</p><p>We need to create index.js, index.js, index.js, index.js, App.js, Header.js, etc similar to below directory structure:</p><pre>react-boilerplate<br>|__index.js <br>|__package.json<br>|<br>|<br>|__templates<br>|          |__templates.js<br>|          |__store.js<br>|          |__MyApp.js<br>|          |__App.js<br>|          |__Footer.js<br>|          |__Header.js<br>|          |__actions.js<br>|          |__index.js<br>|          |__reducers.js</pre><p>Now you will be thinking why these file required, these files will help us to create a directory structure and in adding redux. Let’s what these files should have.</p><p>We will start from index.js</p><pre>module.exports = `<br>    import React from &#39;react&#39;;<br>    import ReactDOM from &#39;react-dom&#39;;<br>    import App from &#39;./container/App&#39;;<br>    import store from &#39;./store&#39;;<br>    import { Provider } from &#39;react-redux&#39;;<br>    import { BrowserRouter as Router } from &#39;react-router-dom&#39;;<br>    ReactDOM.render(&lt;Router&gt;<br>          &lt;Provider store={store}&gt;<br>             &lt;App /&gt;<br>          &lt;/Provider&gt;<br>        &lt;/Router&gt;, <br>       document.getElementById(&#39;root&#39;));<br>`</pre><p>We have create a simple index file which will hav redux and store object imported from our respective directories which are getting created with the help of update templates function and we are mentioning what should be the directory like store/index.js .</p><p>Now let’s wrap it and complete all other files as below:</p><p>App.js will be something like this</p><pre>module.exports = `<br>    import React, { Component } from &#39;react&#39;;<br>    import { connect } from &#39;react-redux&#39;;<br>    import Header from &#39;../components/Header&#39;;<br>    import Footer from &#39;../components/Footer&#39;;<br>    import MyApp from &#39;../components/MyApp&#39;;</pre><pre>    class App extends Component {  <br>      render() {    <br>        return (      <br>          &lt;div className=&quot;App&quot;&gt;        <br>            &lt;div className=&quot;App-header&quot;&gt;          <br>              &lt;Header /&gt;        <br>            &lt;/div&gt;        <br>            &lt;div className=&quot;App-intro&quot;&gt;          <br>              &lt;MyApp /&gt;        <br>            &lt;/div&gt;        <br>            &lt;div className=&quot;App-footer&quot;&gt;          <br>              &lt;Footer /&gt;        <br>            &lt;/div&gt;      <br>          &lt;/div&gt;    <br>        );  <br>      }<br>    }</pre><pre>    export default connect(  state=&gt;({}),  {})(App)<br>`</pre><p>MyApp.js should have below code:</p><pre>module.exports = `<br>    import React, { Component } from &#39;react&#39;;<br>    import { connect } from &#39;react-redux&#39;;<br>    import styled from &#39;styled-components&#39;;<br>    import { Link } from &#39;react-router-dom&#39;;<br>    import actions from &#39;../actions&#39;;<br>    const Title = styled.h3\`  <br>        font-size: 1.5em;  <br>        text-align: center;  <br>        color: palevioletred;\<br>     `;<br>     const DescriptionPara = styled.p\`  <br>         text-align: center;  <br>         font-size: 1em;\<br>     `; <br>     const Wrapper = styled.section\`  <br>          padding: 0em;  <br>          background: papayawhip;\<br>     `;<br>      const AppWrapper = styled.div\`  <br>           width:48px;  <br>           height:48px;  <br>           padding-left: 20px;\<br>      `;<br>    class MyApp extends Component {  <br>      render(){    <br>        return(     <br>          &lt;AppWrapper&gt;        <br>            &lt;Title&gt;Start your next react project in seconds by using react-redux-scss-setup<br>            &lt;/Title&gt;<br>            &lt;div&gt;<br>              &lt;Title&gt; Try Me ...&lt;/Title&gt;<br>            &lt;/div&gt;        <br>            &lt;DescriptionPara&gt;<br>              &lt;Link to={&#39;https://github.com/hnagarkoti/react-redux-biolerplate&#39;}&gt;<br>                Click to view Github repositry<br>              &lt;/Link&gt;        <br>            &lt;/DescriptionPara&gt;      <br>         &lt;/AppWrapper&gt;    <br>       )  <br>      }<br>     }</pre><pre>const mapStateToProps = (state) =&gt; {  <br>    return {<br>      state  <br>    };<br>};<br>export default connect(mapStateToProps, {...actions})(MyApp);<br>`</pre><p>Footer.js will look like</p><pre>module.exports = `<br>    import React, { Component } from &#39;react&#39;;<br>    import { connect } from &#39;react-redux&#39;;<br>    import styled from &#39;styled-components&#39;;<br>    import actions from &#39;../actions&#39;;<br>    const Title = styled.h1\`  <br>        font-size: 1.5em;  <br>        text-align: center;  <br>        color: palevioletred;\<br>    `;<br>    const Wrapper = styled.section\`  <br>        padding: 0em;  <br>        background: papayawhip;\<br>    `;<br>    const CopyRight = styled.p\`  <br>        font-size: medium;  <br>        background-color: yellowgreen\<br>    `;  <br>    class Footer extends Component {  <br>      render(){    <br>        const date1 = new Date(&#39;05/07/2019&#39;);    <br>        const date2 = new Date();    <br>        const diffTime = Math.abs(date2.getTime() - date1.getTime());    <br>        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));    <br>        return(      <br>          &lt;Wrapper&gt; <br>            &lt;Title&gt;Footer Section&lt;/Title&gt;<br>            &lt;CopyRight&gt;{\`Created By Hemant Nagarkoti \${diffDays} days back\`}        <br>            &lt;/CopyRight&gt;<br>          &lt;/Wrapper&gt;    <br>         )  <br>      }<br>     }</pre><pre>const mapStateToProps = (state) =&gt; {  <br>    return {    <br>      state  <br>    };<br>};</pre><pre>export default connect(mapStateToProps, {...actions})(Footer);<br>`</pre><p>Header.js will look like this</p><pre>module.exports = `<br>    import React, { Component } from &#39;react&#39;;<br>    import { connect } from &#39;react-redux&#39;;<br>    import styled from &#39;styled-components&#39;;<br>    import actions from &#39;../actions&#39;;<br>    const Title = styled.h1\`  <br>        font-size: 1.5em;  <br>        text-align: center;  <br>        color: palevioletred;\<br>     `;<br>    const Wrapper = styled.section\`  <br>        padding: 0em;  <br>        background: papayawhip;\<br>    `;</pre><pre>    class Header extends Component {  <br>      render(){    <br>        return(      <br>          &lt;Wrapper&gt;        <br>            &lt;Title&gt;Header Section&lt;/Title&gt;      <br>          &lt;/Wrapper&gt;    <br>         )  <br>       }<br>     }<br>const mapStateToProps = (state) =&gt; {  <br>    return {    <br>        state  <br>    };<br>};<br>export default connect(mapStateToProps, {...actions})(Header);`</pre><p>Now it’s time for creating our action creators ie. actions.js</p><pre>module.exports = `<br>    module.exports = {<br>    <br>    };<br>`</pre><p>Now we will create our reducers file ie. reducers.js</p><pre>module.exports = `<br>    import { combineReducers } from &#39;redux&#39;;<br>    const rootReducer = combineReducers({  state: () =&gt; ({})});<br>    export default rootReducer;<br>`</pre><p>We are almost done with our boilerplate we just need to add store file ie. store.js</p><pre>module.exports = `<br>    import { createStore, combineReducers, applyMiddleware } from &#39;redux&#39;;<br>    import ReduxThunk from &#39;redux-thunk&#39;;<br>    const initialState = {};<br>    const AppReducer = (state=initialState, action) =&gt; {  <br>       switch (action.type) {    <br>         case &#39;ACTION&#39;:      <br>           return state;    <br>         default:      <br>           return state  <br>        }<br>     }</pre><pre>export default createStore(  combineReducers({    <br>     app: AppReducer  <br>    }),  <br>    applyMiddleware(    ReduxThunk  ))<br>`</pre><p>We have done with our boilerplate. Now we just need to install our dependencies by running npm install or yarn install in our directory where package.json is written.</p><p>Now by running below command we can create a global symbolic link in our local machine.</p><pre>npm link</pre><p>Now we can test our boilerplate by opening a new terminal and by running react-redux-boiler-setup appName it will create a new application using our boilerplate. We are done with out boilerplate, we have created a npm package which adds a small wrapper over our create-react-app app.</p><p>Now do cd appName and run npm run start and go to localhost:3000 you should see our application running fine.</p><p>That’s it guys we have done with our boilerplate.</p><p><a href="https://github.com/hnagarkoti/react-redux-biolerplate"><strong>If you want to see my final code, you can find it here.</strong></a></p><p><a href="https://github.com/hnagarkoti/"><strong>Follow me on Github.</strong></a></p><p><a href="https://twitter.com/h_emant007"><strong>Follow me on Twitter.</strong></a></p><p>If you like the post please like it.</p><p>Thanks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d92d677f2b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>