<?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 Sumit Joshi on Medium]]></title>
        <description><![CDATA[Stories by Sumit Joshi on Medium]]></description>
        <link>https://medium.com/@sumitjoshi97?source=rss-1f26a466bdd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*MiVxA3rhGN-GqmylhH0jzw.jpeg</url>
            <title>Stories by Sumit Joshi on Medium</title>
            <link>https://medium.com/@sumitjoshi97?source=rss-1f26a466bdd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:42:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sumitjoshi97/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[Mimic Redux with React Context API and hooks]]></title>
            <link>https://medium.com/swlh/mimic-redux-with-react-context-api-and-hooks-21fbec280205?source=rss-1f26a466bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/21fbec280205</guid>
            <category><![CDATA[global-state]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[context-api]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sumit Joshi]]></dc:creator>
            <pubDate>Mon, 22 Jul 2019 17:47:59 GMT</pubDate>
            <atom:updated>2019-07-23T06:56:38.005Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7bNp1mrLtficfYCp-v7gHA.png" /></figure><p>If you have worked on a React project you should know how sometimes state management can be a real disaster because of prop-drilling. And Redux is usually the most popular way to solve it with its global state management. But with the recent updates in React, it is now possible to manage global state in React itself without using a third party framework like Redux.</p><p>In this article, we will learn to mimic the Redux global state management with React hooks and Context API. But first, let’s check all the prerequisites.</p><h3>Prerequisites</h3><p>You need to have at least understanding of-</p><ul><li>basics of Redux - <em>dispatcher, actions, reducers, etc</em></li><li>React, as we are having a React tutorial ;)</li><li>basic React hooks - <em>useState</em></li></ul><h3>A bit about hooks and Context API</h3><blockquote>Note: If you are familiar with Context API and useReducer hook you can skip to next part.</blockquote><h4>Context API</h4><p>Context API provides a way to pass data across the components without having to pass props down manually at every level.</p><p>You can create a context object with React.createContext -</p><pre>const MyContext = React.createContext(defaultValue)</pre><p>When a component subscribes to this context it will read the current value of context.</p><p>Every context object comes with a <strong>Provider</strong> and <strong>Consumer</strong> Component —</p><ul><li><strong>Provider</strong> allows consuming components to subscribe to context changes.</li></ul><pre>&lt;MyContext.Provider value={/* some value */}&gt;<br>  {/* children */}<br>&lt;/MyContext.Provider&gt;</pre><ul><li><strong>Consumer</strong> allows consuming the value store from the Context Provider.</li></ul><pre>&lt;MyContext.Consumer&gt;<br>  {value =&gt; /* render something based on the context value */}<br>&lt;/MyContext.Consumer&gt;</pre><h4>useContext</h4><p>useContext is a React hook that returns the current value of context object defined in useContext hook.</p><pre>const value = useContext(MyContext)</pre><p>Here the current context value is determined by the value prop of the nearest &lt;MyContext.Provider&gt; above the calling component.</p><h4>useReducer</h4><p>useReducer is an alternative of useState. It is usually preferable to useState when state logic is either complex or when the next state depends on the previous one.</p><pre>const [state, dispatch] = useReducer(reducer, initialState)</pre><p>It accepts a reducer function of type (state, action) =&gt; newState and returns the current state paired with a dispatch method. It is similar to Redux, so if you are familiar with it, you already know how it works.</p><p>Now we are done with the basic introduction to <strong>Context API</strong> and <strong>hooks</strong> that we will be using in this article, so let’s dive into a practical example to understand how it works together.</p><h3>A Practical Example — TODO app</h3><h4>What we are building</h4><p>We are going to build a basic to-do app where you can add/remove a todo and tag a todo as completed.</p><p>In this tutorial, we will first define our state with useReducer hook and then lift it with Context API.</p><h4>Getting Started</h4><p>Let’s start with creating a React project. We can do this using <strong>create-react-app</strong> or some other boilerplate of choice. Here, I will be using <strong>create-react-app</strong> -</p><pre>create-react-app todo-app-global-state</pre><p>Once your project is created, delete unnecessary files - logo.svg, App.test.js and rename App.js to App.jsx.</p><p>Now let’s start with creating our todo components. First, create a components folder inside src folder -</p><pre>cd src<br>mkdir components &amp;&amp; cd components</pre><p>Then, create TodoForm and TodoList components inside components folder-</p><pre>mkdir TodoForm &amp;&amp; touch TodoForm/index.jsx<br>mkdir TodoList &amp;&amp; touch TodoList/index.jsx</pre><p>If the file structure seems confusing, you can check it below -</p><pre>...<br>/src/<br>   |-- /components/<br>       |-- /TodoForm/<br>           |--index.jsx <br>       |-- /TodoList/<br>           |--index.jsx<br>   |-- App.jsx<br>   |-- App.css<br>   |-- index.js<br>   |-- index.css<br>...</pre><p><em>src/components/TodoForm/index.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21347ebddc75e6c24039ed20e4c2b35b/href">https://medium.com/media/21347ebddc75e6c24039ed20e4c2b35b/href</a></iframe><p><em>src/components/TodoList/index.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0401e7fb33766d4084c4c17ec00192a/href">https://medium.com/media/d0401e7fb33766d4084c4c17ec00192a/href</a></iframe><p>Now add these components to App.jsx -</p><p><em>src/App.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddaa2e8dcccc9ff54a0689f7781e05e4/href">https://medium.com/media/ddaa2e8dcccc9ff54a0689f7781e05e4/href</a></iframe><p>With this, our basic app setup is completed and we can move to our main topic setting up a global store.</p><h4>Create a Store</h4><p>Let’s start with creating a store folder which will have our store and actions inside src folder -</p><pre>cd src<br>mkdir store &amp;&amp; cd store </pre><p>Create a Store.jsx file inside store folder -</p><pre>touch Store.jsx</pre><p>Here in Store.jsx we will first create a Store context with React.createContext, then we will work on our reducer function and define our Store Provider component.</p><p><em>src/store/Store.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55bad7d9fe9e0ff19a83e61d8d2ee873/href">https://medium.com/media/55bad7d9fe9e0ff19a83e61d8d2ee873/href</a></iframe><p>Let’s get a quick rundown of things in the above code. We created a -</p><ol><li>Store context with React.createContext</li><li>reducer function - a pure function which takes current state and action object as params and returns the new state after doing calculations on current state based on the action received.</li><li>StoreProvider component - a wrapper component which returns a Provider with value prop of state and dispatch from useReducer hook.</li></ol><p>The next step is to open index.js in src folder and wrap App component in StoreProvider -</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6314fe98478a6958718234b730696727/href">https://medium.com/media/6314fe98478a6958718234b730696727/href</a></iframe><p>With this, our App component and all other components in the component tree can access Store context.</p><h4>Add cases for Reducer</h4><p>The next step is to add cases to the reducer function.</p><p>So let’s start with populating the initialState with a todos array -</p><pre>const initialState = {<br>  todos: []<br>}</pre><p>And adding cases to the reducer function -</p><p><em>src/store/Store.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15e3c0d61030540f1d84ab257fb25313/href">https://medium.com/media/15e3c0d61030540f1d84ab257fb25313/href</a></iframe><p>Here we added cases for adding, removing a todo from the todo list and changing the current todo tag - <em>active/complete.</em></p><h4>Create our Actions</h4><p>With the reducer function completed, actions are the final part to complete our React global state.</p><p>Create Actions.js in store folder inside src folder -</p><pre>touch Actions.js</pre><p><em>src/store/Actions.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c3000eb508698a9c1f3b3864cd44194/href">https://medium.com/media/5c3000eb508698a9c1f3b3864cd44194/href</a></iframe><h4>Connect Global State with Components</h4><p>The next step is to connect our global react to React components so that we can access global state from components.</p><p>Here we will use useContext hook to get the value of Store context we created earlier.</p><p>So let’s open <strong>TodoForm</strong> and <strong>TodoList</strong> component and edit them -</p><p><em>src/components/TodoForm/index.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f36988733e3f2a7593531fe87ad654c8/href">https://medium.com/media/f36988733e3f2a7593531fe87ad654c8/href</a></iframe><p>So here we didn’t change a lot of things, but let’s take a quick look -</p><ol><li>at <strong>line 6</strong>, we added useContext hook to get the dispatch function,</li><li>at <strong>line 16</strong>, we replaced the console log statement with the addTodo action from Actions.js file.</li></ol><p><em>src/components/TodoList/index.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2d220ee1e9165b3a5ee05130864ef51/href">https://medium.com/media/f2d220ee1e9165b3a5ee05130864ef51/href</a></iframe><p>Okay, so let’s get a quick rundown of things we did above -</p><ol><li>at <strong>line 6</strong>, we used useContext hook to get the value of state and dispatch from Store context,</li><li>at <strong>line 8</strong>,<strong> </strong>we created an array - todoList that map todos array to the <strong>Todo </strong>component.</li></ol><p>But we do not have a <strong>Todo</strong> component yet so, let’s create a <strong>Todo</strong> component -</p><pre>cd TodoList<br>touch Todo.jsx</pre><p><em>src/components/TodoList/Todo.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a1065558fe2d5d253e2bcd5d84e5798/href">https://medium.com/media/7a1065558fe2d5d253e2bcd5d84e5798/href</a></iframe><p>Now let’s take a quick view through the Todo component code -</p><ol><li>at <strong>line 8 and 14</strong>, we added our editTag and removeTodoactions to our <strong>Todo</strong> component,</li><li>at <strong>line 19</strong>, we have defined inline style object todoStyle that defines todo style based on the current todo tag.</li><li>Additionally, we wrapped our <strong>Todo</strong> component in React.memo.</li></ol><p>React.memo is a higher-order component (hoc). It’s similar to <a href="https://reactjs.org/docs/react-api.html#reactpurecomponent">React.PureComponent</a> but for function components instead of classes. So what it does is that wrapping a component inside React.memo will not re-render if input props are the same. But it only works if props are not functions, if you are passing functions as props you should use useCallback hook in addition to React.memo.</p><h4>Some Styling</h4><p>Now with everything done, let’s add a bit of styling to our app.</p><p>Edit index.css and App.css -</p><p><em>src/index.css</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cc92cc74541f0e2d2b545f81ce81ab6/href">https://medium.com/media/1cc92cc74541f0e2d2b545f81ce81ab6/href</a></iframe><p><em>src/App.css</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb5ab6c0e4c0eb4dff27501acab417ba/href">https://medium.com/media/fb5ab6c0e4c0eb4dff27501acab417ba/href</a></iframe><p>With this, we have created our todo app using React global state. And what’s left is just to launch the project with -</p><pre>yarn start </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RADL93SF3i_t2NyNQurqCA.jpeg" /><figcaption>Todo App</figcaption></figure><p>And yeah here is the <a href="https://github.com/sumitjoshi97/TODO-APP-global-state-tutorial">link</a> for tutorial repo, if you missed something.</p><h3>Conclusion</h3><p>So there you have it -</p><ol><li>Create an initial state object.</li><li>Create context with const context = React.createContext(initialState)</li><li>Use useReducer hook to get state and dispatch const [state, dispatch] = React.useReducer(reducer, initialState)</li><li>Create reducer function and actions.</li><li>Wrap Provider around parent component.</li><li>Consume context with const {state, dispatch} = React.useContext(context)</li></ol><p>But now the main question arises - can React global state replace Redux completely? And the answer is no for now, as alongside global state Redux also provides a bunch of other features — Redux dev tools, middlewares, ability to combine multiple reducers (no official way to combine React reducers yet) are some of the things I can think of.</p><p>So for your next project, you might need to think about whether you need Redux or, React global state is the right tool for you.</p><p>And lastly, let me know if I got something wrong or if there is something that could be improved or simplified.</p><p>Thanks for reading and happy coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21fbec280205" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/mimic-redux-with-react-context-api-and-hooks-21fbec280205">Mimic Redux with React Context API and hooks</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup a minimal react boilerplate with most features]]></title>
            <link>https://medium.com/swlh/setup-a-minimal-react-boilerplate-with-most-features-84f51c7065d6?source=rss-1f26a466bdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/84f51c7065d6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[boilerplate]]></category>
            <category><![CDATA[starter-kit]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sumit Joshi]]></dc:creator>
            <pubDate>Mon, 01 Jul 2019 18:15:53 GMT</pubDate>
            <atom:updated>2019-07-03T11:51:41.662Z</atom:updated>
            <content:encoded><![CDATA[<h3>Let’s create a Minimal <em>React Starter Kit</em> with most features</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sDki1g16K3aVurli" /><figcaption>Photo by <a href="https://unsplash.com/@codestorm?utm_source=medium&amp;utm_medium=referral">Safar Safarov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>React is a popular JavaScript library for building user interfaces. And while being a popular choice of front-end developers to work on their projects, most of the developers opt to use pre-built starter kits to start with their projects. And I am no exception to this, I also started learning React with <strong>create-react-app, </strong>the holy grail of React starter kits and I still use it for most of my solo projects.</p><p>These starter kits are useful while learning React as developers do not need to concern about React tooling and configuration. But learning React cannot be completed without understanding behind the hood working of a starter-kit or React tooling to say. And there’s no better way to learn it than creating your own starter kit.</p><p>So, let’s start and create our own <em>React Starter Kit</em> from scratch.</p><h3>Getting Started from Scratch</h3><p>Oh, you are still here? That’s good, it means you are also interested in learning behind the hood working of a starter kit.</p><p>Let’s start building our <em>React Starter Kit</em> project. For pre-requisites, we only require a terminal, code editor, node and yarn (or npm, if you prefer that).</p><blockquote>I will be using yarn package manager throughout the tutorial but you can use npm, just replace yarn with npm in terminal commands.</blockquote><p>We will be using typical folder structure for our <em>starter kit</em> -</p><pre>react-minimal/<br>   |-- /__mocks__/ - mock files for jest<br>           |-- styleMock.js<br>           |--fileMock.js<br>   |-- /public/<br>           |-- index.html<br>           |-- favicon.ico<br>   |-- /src/<br>           |-- App.jsx<br>           |-- index.js<br>           |-- setupTests.js<br>   |-- postcss.config.js<br>   |-- jest.config.js<br>   |-- webpack.config.base.js<br>   |-- webpack.config.dev.js<br>   |-- webpack.config.prod.js<br>   |-- package.json<br>   |-- .babelrc<br>   |-- .prettierrc<br>   |-- .eslintrc<br>   |-- .gitignore</pre><p>So, let’s start with creating a project folder and app initialization with yarn -</p><pre>mkdir react-minimal &amp;&amp; cd react-minimal<br>yarn init -y</pre><p>Next, we need an entrypoint HTML file and a favicon to serve our <em>starter kit</em> in the browser.</p><p>So, create a public folder and add index.html inside it -</p><pre>mkdir public &amp;&amp; cd public<br>touch index.html</pre><p><em>index.html</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/979d6a17757d921dac6c59cd909d0947/href">https://medium.com/media/979d6a17757d921dac6c59cd909d0947/href</a></iframe><p>You can get favicon that I am using from <a href="https://github.com/sumitjoshi97/react-minimal/blob/master/public/favicon.ico">here</a> or generate one for yourself from <a href="https://favicon.io/">here</a>. Once downloaded, move favicon to the public folder.</p><p>Now our basic stuff is done, we can move to a more exciting part -<strong> webpack</strong>.</p><h3>1. Setup Webpack</h3><p>Webpack is a famous bundler which bundles all your files and outputs static files. And we will be using it bundle our project files - React files, stylesheets, images, etc. If you want to learn more about webpack, you can read <a href="https://webpack.js.org/concepts">here</a>.</p><p>Now let’s install webpack and some other packages -</p><pre>yarn add webpack webpack-cli webpack-dev-server webpack-merge babel-loader eslint-loader html-webpack-plugin html-loader dotenv-webpack style-loader css-loader sass-loader postcss-loader file-loader-D</pre><p>We will be creating 3 webpack files -</p><ol><li>webpack.config.base.js<strong> : </strong>config common for both development and production environment.</li><li>webpack.config.dev.js <strong>: </strong>config for development environment, includes hot reloading, etc.</li><li>webpack.config.prod.js : config for production environment, includes code optimization, etc.</li></ol><p>Now, create these files inside the root folder -</p><pre>touch webpack.config.base.js webpack.config.dev.js webpack.config.prod.js</pre><p><em>webpack.config.base.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/217b18589c96bd02cfe6a843e50802c3/href">https://medium.com/media/217b18589c96bd02cfe6a843e50802c3/href</a></iframe><p>In webpack development config, we will be adding hot reloading, sass support and other features.</p><p><em>webpack.config.dev.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1445b3f04d318569ad5645d0457fd568/href">https://medium.com/media/1445b3f04d318569ad5645d0457fd568/href</a></iframe><p>In webpack production config, we will work on production code optimization. But before that, we need to install some packages -</p><pre>yarn add mini-css-extract-plugin terser-webpack-plugin optimize-css-assets-webpack-plugin postcss-safe-parser clean-webpack-plugin -D</pre><p><em>webpack.config.prod.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad294a74d0d42cf87e3211b4860a7846/href">https://medium.com/media/ad294a74d0d42cf87e3211b4860a7846/href</a></iframe><p>With this our webpack configuration is done, but we need to add a <em>postcss </em>config file before diving into the next part of our <em>React Starter Kit</em>.</p><p>Create postcss.config.js in root folder -</p><pre>touch postcss.config.js</pre><p><em>postcss.config.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4db7f42fe10a394f6a18701c440c72d/href">https://medium.com/media/e4db7f42fe10a394f6a18701c440c72d/href</a></iframe><p>And, with this, our webpack setup is done.</p><h3>2. Babel Configuration</h3><p>React uses JSX and ES6 syntax, but the browser cannot understand it. So, we need babel which will transform React code to ES5 code that browser can understand.</p><p>Let’s add babel to our <em>starter kit</em> but first, we need to add some dependencies -</p><pre>yarn add @babel/core @babel/preset-react @babel/preset-env @babel/plugin-proposal-class-properties @babel/plugin-proposal-function-bind @babel/plugin-syntax-dynamic-import babel-plugin-transform-react-remove-prop-types -D</pre><p>And create a babel configuration in root folder -</p><pre>touch .babelrc</pre><p><em>.babelrc</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac72ca5b23593b91daa3905783ecbe9f/href">https://medium.com/media/ac72ca5b23593b91daa3905783ecbe9f/href</a></iframe><p>With this, our babel configuration is done and now we can write some react code. So, let’s try creating a React component.</p><p>Create a<em> </em><em>src </em>folder and add index.js entrypoint file and a React app component App.jsx inside src folder -</p><pre>mkdir src &amp;&amp; cd src<br>touch index.js App.jsx</pre><p><em>src/index.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7449e3d20c24ab64e8d1fb255cdc73a9/href">https://medium.com/media/7449e3d20c24ab64e8d1fb255cdc73a9/href</a></iframe><p><em>src/App.jsx</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1086635ab423fcb2c4a9b0f391fba793/href">https://medium.com/media/1086635ab423fcb2c4a9b0f391fba793/href</a></iframe><p>Also, add some scripts in package.json to run React code -</p><p><em>package.json</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78070a38fad53ae500ab5b647296c436/href">https://medium.com/media/78070a38fad53ae500ab5b647296c436/href</a></iframe><p>Now, let’s start <em>starter kit</em> in development mode -</p><pre> yarn start</pre><p>This should open<em> starter kit</em> at <a href="http://localhost:3000/">http://localhost:3000</a> in the browser.</p><h3>3. Test with Jest</h3><p>Testing is an important aspect of software development and our <em>starter kit </em>cannot be completed without adding a testing environment. So, let’s add the testing environment to our <em>starter kit.</em></p><p>Jest is one of the most popular testing frameworks when talking about testing React components. So, we will be using <strong>Jest</strong> and a testing assertion utility <strong>Enzyme</strong>(makes easier to test React components) in our <em>starter kit</em> to setup a testing environment.</p><p>First, let’s install jest and enzyme dependencies -</p><pre>yarn add jest jest-cli enzyme enzyme-adapter-react-16 -D</pre><p>Now, create jest.config.js in root folder -</p><pre>touch jest.config.js</pre><p><em>jest.config.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d80d447b4cc35a1928befa8802fcf530/href">https://medium.com/media/d80d447b4cc35a1928befa8802fcf530/href</a></iframe><p>In our jest config, we used a property named <em>moduleNameMapper, </em>it<em> </em>mocks the import files which are not native javascript or jsx files, such as CSS, image or other asset files. So, we need to create mock files for these.</p><p>Create <em>__mocks__</em> directory inside the root folder of the project and add mock files inside it -</p><pre>mkdir __mocks__ &amp;&amp; cd __mocks__<br>touch styleMock.js fileMock.js</pre><p><em>__mocks__/styleMock.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63ad85a62d2fcd1e19686f94caf387c5/href">https://medium.com/media/63ad85a62d2fcd1e19686f94caf387c5/href</a></iframe><p><em>__mocks__/fileMock.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61933031361180cb39fdf01e8ff56f92/href">https://medium.com/media/61933031361180cb39fdf01e8ff56f92/href</a></iframe><p>But, that’s not all, we also need to setup Enzyme, so that we don’t have to write redundant code for every test suite.</p><p>Create setupTests.js inside src folder -</p><pre>cd src<br>touch setupTests.js</pre><p><em>src/setupTests.js</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/351597ec1eb348cc74674178d96ddd9e/href">https://medium.com/media/351597ec1eb348cc74674178d96ddd9e/href</a></iframe><p>Now, let’s add some scripts in our package.json<em> </em>to complete our testing environment setup -</p><p><em>package.json</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57518a7771a5ade940a1de65dcc876d1/href">https://medium.com/media/57518a7771a5ade940a1de65dcc876d1/href</a></iframe><p>With this, our testing environment is done and we can move to the next part of our <em>starter kit.</em></p><h3>4. Lint code with ESLint</h3><p>When talking about linting, the first thing that comes into mind is usually style guides. But linting is not limited to just style guides, it also helps in avoiding certain classes of errors — variable scopes, assignment errors, etc.</p><p>ESLint is a popular tool for linting JavaScript code and can be used to enforce different configurations. Here we will be using ESLint config by Airbnb with a few touches here and there.</p><p>So, let’s install some dependencies first -</p><pre>yarn add babel-eslint eslint eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jest eslint-plugin-jsx-a11y eslint-plugin-prettier eslint-plugin-react -D</pre><p>Once dependencies are installed, create .eslintrc in root folder -</p><pre>touch .eslintrc</pre><p><em>.eslintrc</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccbbf768be5c9ef582ddb4ee9a3aacbc/href">https://medium.com/media/ccbbf768be5c9ef582ddb4ee9a3aacbc/href</a></iframe><p>Also, add a script for linting in package.json -</p><p><em>package.json</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9b6eb7e6ff8addf23df52ecf2be2926/href">https://medium.com/media/c9b6eb7e6ff8addf23df52ecf2be2926/href</a></iframe><p>With this, our linter setup is done but we also need a code formatter, that will format our code. As prettier is an opinionated code formatter for most. So, let’s use prettier for our project.</p><p>Create a prettier config in the root folder -</p><pre>touch .prettierrc</pre><p><em>.prettierrc</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2fdcce558e020839a586ed94050bc53/href">https://medium.com/media/c2fdcce558e020839a586ed94050bc53/href</a></iframe><p>If you use VSCode, you can install ESLint and Prettier extension from here:</p><ul><li><a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">ESLint - Visual Studio Marketplace</a></li><li><a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode">Prettier - Code formatter - Visual Studio Marketplace</a></li></ul><p>Once extensions are installed, click cmd + (in Mac) or ctrl+, (in Windows) to open VSCode settings and add this line to workspace settings: &quot;editor.formatOnSave&quot;:true to format your code automatically every time you save.</p><h3>5. Add Pre-Commit Hooks with Husky</h3><p>It is always best to make sure that your code is clean and error-free before you push it to source control (aka <strong><em>git</em></strong>). And, that’s what husky do for us. So, let’s add husky in our project.</p><pre>yarn add husky lint-staged -D</pre><p>And, then configure it inside package.json -</p><p><em>package.json</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/324ff40713d67cc1b5dcdf1e8c12910b/href">https://medium.com/media/324ff40713d67cc1b5dcdf1e8c12910b/href</a></iframe><p>You can use husky to do a ton of things, like running prettier, tests before pushing changes to git. But we will be stopping here for now.</p><h3>Conclusion</h3><p>Congratulations! Finally, we’ve completed our <em>React Starter Kit</em> project. Though it’s far from over, you can try to add more features such as typescript integration, routers, adding PWA support. There are no bounds to it, but that’s all for today.</p><p>And, Of course, let me know if I got something wrong or if there is something that could be improved or simplified. PR’s and issues are more than welcome!</p><p>Lastly, here is the link for this <a href="https://github.com/sumitjoshi97/react-minimal"><strong>article repo</strong></a><strong>.</strong></p><p>Thanks for reading! Happy Coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84f51c7065d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/setup-a-minimal-react-boilerplate-with-most-features-84f51c7065d6">Setup a minimal react boilerplate with most features</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>