<?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: AnkitPat</title>
    <description>The latest articles on DEV Community by AnkitPat (@ankitpat).</description>
    <link>https://dev.to/ankitpat</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%2F771440%2F546533bf-6ccc-4f32-bf95-31eaf796257e.png</url>
      <title>DEV Community: AnkitPat</title>
      <link>https://dev.to/ankitpat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ankitpat"/>
    <language>en</language>
    <item>
      <title>Write your own custom hook for Favourite count.</title>
      <dc:creator>AnkitPat</dc:creator>
      <pubDate>Wed, 27 Apr 2022 12:15:56 +0000</pubDate>
      <link>https://dev.to/ankitpat/write-your-own-custom-hook-for-favourite-count-2ace</link>
      <guid>https://dev.to/ankitpat/write-your-own-custom-hook-for-favourite-count-2ace</guid>
      <description>&lt;p&gt;So with react latest versions, now you are getting more features like hooks. By which you can achieve things more easily and will less code ofcourse.&lt;/p&gt;

&lt;p&gt;In many cases, if we need to have any functionality for our application. There are a-lot of libraries, packages available for doing same things. But if there is no hook or library exist? What do you do?&lt;/p&gt;

&lt;p&gt;As a React developer, it's important to learn the process of creating custom hooks to solve problems or add missing features within your own React projects.&lt;/p&gt;

&lt;p&gt;In this step-by-step guide, I will show you how to create your own custom React hooks from writing word by word.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom hooks:&lt;/strong&gt;&lt;br&gt;
Custom hooks are just an javascript functions with some variable and functions which we can export to be called from outside and variable to access newest value of changed things.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-1 : Create a dummy app as "custom-hooks"&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app custom-hooks&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After successfully completion of above command to make sure your app is working.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd custom-hooks

npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step-2 : Make sure your app is running now on localhost:3000 *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fgMOSGp1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/019ftbo2joc9bhbgt9af.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fgMOSGp1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/019ftbo2joc9bhbgt9af.png" alt="Image description" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**Step-3 : Create a file "useFavouriteCount.js" to write our custom hook logic.&lt;/p&gt;

&lt;p&gt;Go to the src folder and create file “useFavouriteCount.js” to write the code for custom Hook and write the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from 'react';
function useFavouriteCountHook() {
   const [favouriteCount, setFavouriteCount] = useState({ count: 0 });

   const changeFavouriteCount = () =&amp;gt; {
      setFavouriteCount({ count: favouriteCount.count + 1 }) 
    }
   return { favouriteCount, changeFavouriteCount };
}
export default useFavouriteCountHook;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's decompile the code first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We're importing &lt;em&gt;useState&lt;/em&gt; in below line:
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;p&gt;Created a function/hook with name "useFavouriteCountHook":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useFavouriteCountHook() {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using useState we should create a new state with “favouriteCount” and initialize with an object “{ count: 0 }”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [favouriteCount, setFavouriteCount] = useState({ count: 0 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, as i said earlier we have to define a variable and a function. Variable we defined using useState, lets define function to update our state values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const changeFavouriteCount = () =&amp;gt; {
      setFavouriteCount({ count: favouriteCount.count + 1 }) 
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now return variable and functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return { favouriteCount, changeFavouriteCount };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As stated earlier, this function should be exposed/exported so other component can import it and use it as hook.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-4 : Using our custom hook in our code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open src/app.js, where we have to import custom hook from &lt;em&gt;useFavouriteCountHook&lt;/em&gt; file.&lt;/p&gt;

&lt;p&gt;Your code should look like these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import useFavouriteCount from './useFavouriteCount';
import './App.css';
function App() {
  const favouriteHook = useFavouriteCount();
  return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;count:{favouriteHook.favouriteCount.count}&amp;lt;/h1&amp;gt;
        &amp;lt;button type='button' onClick=
                {favouriteHook.increaseFavouriteCount}&amp;gt;Increment&amp;lt;/button&amp;gt;

          &amp;lt;button type='button' onClick=
                  {favouriteHook.decreaseFavouriteCount}&amp;gt;Decrement&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
  );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can use it like these as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import useFavouriteCount from './useFavouriteCount';
import './App.css';
function App() {
  const {favouriteCount, increaseFavouriteCount, decreaseFavouriteCount} = useFavouriteCount();
  return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;count:{favouriteCount.count}&amp;lt;/h1&amp;gt;
        &amp;lt;button type='button' onClick=
                {increaseFavouriteCount}&amp;gt;Increment&amp;lt;/button&amp;gt;

          &amp;lt;button type='button' onClick=
                  {decreaseFavouriteCount}&amp;gt;Decrement&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
  );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step-5 : Run the app and see the browser using localhost:3000: *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rljNEquB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa5qn9z0m5zksquyd6c9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rljNEquB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa5qn9z0m5zksquyd6c9.gif" alt="Demo" width="600" height="648"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also find above code on &lt;a href="https://github.com/AnkitPat/react_custom_hook"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
In this blog, you learned the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;About custom hook.&lt;/li&gt;
&lt;li&gt;Created your own hook.&lt;/li&gt;
&lt;li&gt;Used same hook in component with two different ways.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React Optimised  Components</title>
      <dc:creator>AnkitPat</dc:creator>
      <pubDate>Sat, 12 Feb 2022 06:32:27 +0000</pubDate>
      <link>https://dev.to/ankitpat/react-optimised-components-5787</link>
      <guid>https://dev.to/ankitpat/react-optimised-components-5787</guid>
      <description>&lt;p&gt;Everyone wonders why react web app runs slow? Answer to these often lies within component only, when and how much its re-rendering. Are those re-render are even necessary? &lt;strong&gt;React&lt;/strong&gt;  doesn't provide you magical performance upgrade, it just gives you tools and way so by which you can optimise it. Now it's on us when and how to use it. Let's get going...&lt;/p&gt;

&lt;p&gt;So since react introduction of virtual DOM, it changed the way of thinking in web developers. With Virtual DOM, react makes UI update as efficient as it should be expected.&lt;/p&gt;

&lt;p&gt;Now, to make React app acts how it should be then you got to understand. How React components renders? How it went through all lifecycle methods in different phases? How and when to use which lifecycle method?&lt;/p&gt;

&lt;p&gt;With react, you can gain alot of performance improvement that it has to offer by measuring and computing performance. And, React provides just the tools and functionalities necessary to make this easy.&lt;/p&gt;

&lt;p&gt;So, lets start with &lt;strong&gt;How React works?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How does React works?
&lt;/h2&gt;

&lt;p&gt;Before we check on optimisation techniques, lets check how react actually works. At the start of React development, you have the simple and obvious JSX syntax, and React’s ability to build and compare virtual DOMs. Since its release, React has influenced many other front-end libraries. Libraries such as Vue.js also rely on the idea of virtual DOMs.&lt;/p&gt;

&lt;p&gt;Here is how React works:&lt;/p&gt;

&lt;p&gt;Each React application begins with a root component, and is composed of many components in a tree formation. Components in React are “functions” that render the UI based on the data (props and state) it receives.&lt;/p&gt;

&lt;p&gt;We can symbolize this as &lt;code&gt;F&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UIView = F(data)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Users interact with the UI and cause the data to change. Interaction can involves clicking a button, tapping on an image, dragging list items around, AJAX requests invoking APIs, etc., all these only changes the data. They never cause the UI to change directly.&lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2Flp94wmmldv3dqoylkfah.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%2Fuploads%2Farticles%2Flp94wmmldv3dqoylkfah.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, data defines state of web application, not just what you have stored in your database. Even bits of frontend states like checkbox state or tab selection are all part of data.&lt;/p&gt;

&lt;p&gt;Whenever there is a change in this data, React uses the component functions to re-render the UI, but only virtually:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UI1 = F(data1)&lt;br&gt;
 UI2 = F(data2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now React compares the difference between new UI with old UI like these:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Changes = Diff(UI1, UI2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now after the difference found, react will only apply those difference to the Real UI Browser. This process is called as &lt;strong&gt;Reconciliation&lt;/strong&gt; .&lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2Fu658cy5q9qxs0h8ebmpe.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%2Fuploads%2Farticles%2Fu658cy5q9qxs0h8ebmpe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This repeated process of diffing and applying changes to browser is going on for every data or state changes in application. These continuous changes and rendering may be one of primary source of performance issue  in any React app. Building a React app where the diffing algorithm fails to reconcile effectively, causing the entire app to be rendered repeatedly can result in a frustratingly slow experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to work on Optimising?
&lt;/h2&gt;

&lt;p&gt;First question is, where exactly we can optimize?&lt;/p&gt;

&lt;p&gt;As you know, during the initial render process, React builds a DOM tree like this:&lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2Fuwqzbix8fmn2l1ahr50f.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%2Fuploads%2Farticles%2Fuwqzbix8fmn2l1ahr50f.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given a part of the data changes, what we want React to do is re-render only the components that are directly affected by the change (and possibly skip even the diff process for the rest of the components):&lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2F86cfuiaw2hafj2tj4bue.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%2Fuploads%2Farticles%2F86cfuiaw2hafj2tj4bue.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, what React ends up doing is:&lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2Fc52ojbl8pu4pgx31a2jo.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%2Fuploads%2Farticles%2Fc52ojbl8pu4pgx31a2jo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, all of the green nodes are rendered and diffed, resulting in wasted time/computation resources. This is where we will primarily put our optimization efforts in. Configuring each component to only render-diff when it is necessary will allow us to reclaim these wasted CPU cycles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things we can do for optimisation:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. ShouldComponentUpdate() ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As your app grows, attempting to re-render and compare the entire virtual DOM at every action will eventually slow down.&lt;/p&gt;

&lt;p&gt;React provides a lifecycle method which can help you in stopping rendering of component which is not actually required to re-render if certain data/state changes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function shouldComponentUpdate(nextProps, nextState) {&lt;br&gt;
    return true;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By default nature of these method is to return true always. Which means giving permission to re-render of component on every change.&lt;/p&gt;

&lt;p&gt;We can modify these method to return false so it will re-render component. But this is not the perfect way to stop re-render. Because it will stop re-rendering of every data change.&lt;/p&gt;

&lt;p&gt;So let's do it more perfect way. You can compare nextState with current state and nextProps to current props. Like these:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function shouldComponentUpdate(nextProps, nextState) {&lt;br&gt;
    return nextProps.Id !== this.props.Id;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Using a React.PureComponent&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To ease and automate a bit this optimization technique, React provides what is known as “pure” component. A &lt;code&gt;React.PureComponent&lt;/code&gt; is exactly like a &lt;code&gt;React.Component&lt;/code&gt; that implements a &lt;code&gt;shouldComponentUpdate()&lt;/code&gt; function with a shallow prop and state comparison.&lt;/p&gt;

&lt;p&gt;A React.PureComponent is more or less equivalent to this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class MyComponent extends React.Component {&lt;br&gt;
     shouldComponentUpdate(nextProps, nextState) {&lt;br&gt;
          return shallowCompare(this.props, nextProps) &amp;amp;&amp;amp; &lt;br&gt;
          shallowCompare(this.state, nextState);&lt;br&gt;
      }&lt;br&gt;
      …&lt;br&gt;
  }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you see it only do shallow comparison, so it will be only effective if your props and state contains primitive data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Making data immutable&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Assuming you have used &lt;code&gt;React.PureComponent&lt;/code&gt; but you still have ways where we have complex data set and cannot be detected by shallow comparison. Other workaround is create Immutable objects.&lt;/p&gt;

&lt;p&gt;The idea behind using immutable data structures is simple. Whenever an object containing complex data changes, instead of making the changes in that object, create a copy of that object with the changes. This makes detecting changes in data as simple as comparing the reference of the two objects.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;Object.assign&lt;/code&gt; or &lt;code&gt;_.extend&lt;/code&gt; (from Underscore.js or Lodash):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;const newValue2 = Object.assign({}, oldValue);&lt;br&gt;
const newValue2 = _.extend({}, oldValue);&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Even better, you can use a library that provides immutable data structures:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;var map1 = Immutable.Map({a:1, b:2, c:3});&lt;br&gt;
var map2 = map1.set('b', 2);&lt;br&gt;
assert(map1.equals(map2) === true);&lt;br&gt;
var map3 = map1.set('b', 50);&lt;br&gt;
assert(map1.equals(map3) === false);&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, Immutable.Map is provided by the library &lt;a href="https://immutable-js.github.io/immutable-js/" rel="noopener noreferrer"&gt;Immutable.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Every time a map is updated with its method set, a new map is returned only if the set operation changed the underlying value. Otherwise, the same map is returned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Using the production build checks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When developing a React app, you are presented with really useful warnings and error messages. These make identifying bugs and issues during development a bliss. But they come at a cost of performance.&lt;/p&gt;

&lt;p&gt;If you look into React’s source code, you will see a lot of if &lt;code&gt;(process.env.NODE_ENV != 'production')&lt;/code&gt; checks. These chunks of code that React is running in your development environment isn’t something needed by the end user. For production environments, all of this unnecessary code can be discarded.&lt;/p&gt;

&lt;p&gt;If you bootstrapped your project using &lt;code&gt;create-react-app&lt;/code&gt;, then you can simply run &lt;code&gt;npm run build&lt;/code&gt; to produce the production build without this extra code. If you are using Webpack directly, you can run &lt;code&gt;webpack -p&lt;/code&gt; (which is equivalent of &lt;br&gt;
&lt;code&gt;webpack --optimize-minimize --define process.env.NODE_ENV="'production'"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Binding function with Context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is very common to see functions bound to the context of the component inside the render function. This is often the case when we use these functions to handle events of child components.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Creates a new&lt;/code&gt;handleUpload&lt;code&gt;function during each render()&lt;br&gt;
&amp;lt;Header onLogoClick={this.handleClick.bind(this)} /&amp;gt;&lt;br&gt;
// ...as do inlined arrow functions&lt;br&gt;
&amp;lt;Header onLogoClick={event =&amp;gt; this.handleClick(event)} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will cause the &lt;code&gt;render()&lt;/code&gt; function to create a new function on every render. A much better way of doing the same is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;class App extends React.Component {&lt;br&gt;
    constructor(props) {&lt;br&gt;
        super(props);&lt;br&gt;
        this.handleClick = this.handleClick.bind(this);&lt;br&gt;
    }&lt;br&gt;
    render() {&lt;br&gt;
       ...&lt;br&gt;
       &lt;br&gt;
       ...&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>optimisation</category>
      <category>reactdom</category>
      <category>reactaddonsperf</category>
    </item>
    <item>
      <title>How to secure API Key on FrontEnd</title>
      <dc:creator>AnkitPat</dc:creator>
      <pubDate>Mon, 13 Dec 2021 04:14:50 +0000</pubDate>
      <link>https://dev.to/ankitpat/how-to-secure-api-key-on-frontend-3c6j</link>
      <guid>https://dev.to/ankitpat/how-to-secure-api-key-on-frontend-3c6j</guid>
      <description>&lt;p&gt;A-lot of times, where we just want to create website to do some dynamic functionalities without being handhold by backend.&lt;/p&gt;

&lt;p&gt;Best way of doing it, is by API Calls. For doing API calls we must be having API keys to work with. &lt;/p&gt;

&lt;p&gt;And now biggest question, where to place API keys.&lt;/p&gt;

&lt;p&gt;What we are going to build today:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FD6ebI_v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16xtp7jhw9772dkw8h4r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FD6ebI_v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16xtp7jhw9772dkw8h4r.gif" alt="Image description" width="600" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets do one thing, first get the API keys. So in these blog posts i am going integrate Weather Forecasting.&lt;/p&gt;

&lt;p&gt;To create Weather forecasting page, we need to work with API. So for these i decided on &lt;a href="https://rapidapi.com/community/api/open-weather-map/"&gt;Open Weather map&lt;/a&gt; to fetch data. So for these you need to create account that will provide access to APIs and api key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aG0uTrbn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfo5trrnas93l6kdjrz1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aG0uTrbn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfo5trrnas93l6kdjrz1.png" alt="Image description" width="880" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So API KEY again, now we have our own API key to access apis from Weather Statistics.&lt;/p&gt;

&lt;p&gt;So again question, how to secure these API key. &lt;br&gt;
So solution is &lt;a href="https://korconnect.io/"&gt;KorConnect&lt;/a&gt; which gives you option to create API Connections.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cGbfvO78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/he34w6vuxi7haadzr2ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cGbfvO78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/he34w6vuxi7haadzr2ts.png" alt="Image description" width="880" height="802"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you need to add API key detail, api url and other information. Once you are done, you will see list of endpoints added.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EL8P7YU---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvjl3lo03j7k9rmd8pmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EL8P7YU---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bvjl3lo03j7k9rmd8pmz.png" alt="Image description" width="880" height="87"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, once you are in. You can see url endpoint and public key to call you secure API call.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CpDKWIiC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u74skstc37vlrkkp77zl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CpDKWIiC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u74skstc37vlrkkp77zl.png" alt="Image description" width="880" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can now call api as, &amp;lt; SECURE_BASE_URL &amp;gt;/endpoint from Statistics. And remember to add headers also.&lt;/p&gt;

&lt;p&gt;Point to note here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you are testing on postman, you should be on Testing Mode.&lt;/li&gt;
&lt;li&gt;When you are all ready to live, turn to Production mode. which is more secured.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Code snippet example to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("YOUR_PUBLIC_URL", {
    "method": "GET",
    "headers": {
        "x-rapidapi-key": "YOUR_PUBLIC_API_KEY"
    }
})
.then(response =&amp;gt; {
    console.log(response);
})
.catch(err =&amp;gt; {
    console.error(err);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now KorConnect will internal connect with your domain, and your API key is secured from outside world.&lt;/p&gt;

&lt;p&gt;It is as simple as that, now you can integrate apis without any additional setup or library.&lt;/p&gt;

&lt;p&gt;You can add more security to your API and API key via &lt;a href="https://kor-comunity.gitlab.io/kor-connect/adir/SECURITY.html"&gt;Read here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>security</category>
    </item>
  </channel>
</rss>
