{"id":8585,"date":"2019-09-23T11:22:33","date_gmt":"2019-09-23T11:22:33","guid":{"rendered":"https:\/\/ui-lib.com\/?p=8585"},"modified":"2022-06-19T08:10:12","modified_gmt":"2022-06-19T08:10:12","slug":"react-techniques-with-performance","status":"publish","type":"post","link":"https:\/\/ui-lib.com\/blog\/react-techniques-with-performance\/","title":{"rendered":"React Techniques with Performance"},"content":{"rendered":"\n<p>Are you a React developer looking forward to knowing about React Techniques with Performance. Don&#8217;t worry. Here, we are going to give you exactly what you&#8217;re looking for. <\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">React Techniques with Performance<\/h2>\n\n\n\n<p>I prefer using <a href=\"https:\/\/code.visualstudio.com\/Download\" target=\"_blank\" rel=\"noopener\">Visual Studio<\/a>. For React development you can use the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=burkeholland.simple-react-snippets\" target=\"_blank\" rel=\"noopener\">Simple React Snippet<\/a> extension in VsCode (Visual Studio). To use this extension facilities like autosuggestion or pre-built snippet you have to save your file as <strong><em>.jsx<\/em><\/strong> (eg. MyFile.<strong><em>jsx<\/em><\/strong>). Follow the above link to get snippet shortcuts to code faster.<\/p>\n\n\n\n<p><strong>Important:<\/strong> If you are using any secret key\/client key in your app, then it&#8217;s better not to use them inside your source folder or in the index.html file. Because this can easily expose to others. So, what you can do is add a file with the name  <strong><em>.env<\/em><\/strong> and put your secret\/client keys there.  <\/p>\n\n\n\n<p><strong><em>.env<\/em><\/strong> file will look like below<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror cm-s-material\" data-setting=\"{&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true}\">CLIENT_KEY = AIzaSyAYdp6JWy6aD-2Jiil3ggePxDSBDIMpD-KF<\/pre><\/div>\n\n\n\n<p>To use these keys inside your app you have to use process.env.CLIENT_KEY. Don&#8217;t forget to add this .env file in .gitignore to avoid sharing these keys in Github.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-invoke-child-function-from-parent\">Invoke child function from parent:<\/h2>\n\n\n\n<p>We may face situations where we need to invoke a child function from parent. Suppose, we have two components named <strong><em>Parent.jsx<\/em>&nbsp;<\/strong>and&nbsp;<em><strong>Child.jsx<\/strong><\/em>. In our <em><strong>Child<\/strong> <\/em>component, we have a function name <em><strong>triggerChild()<\/strong><\/em> which will invoke from the <em><strong>Parent<\/strong> <\/em>component. Let&#8217;s have a look at the files\/components below<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror cm-s-default\" data-setting=\"{&quot;mode&quot;:&quot;jsx&quot;,&quot;mime&quot;:&quot;text\/jsx&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true}\">import&nbsp;React,&nbsp;{&nbsp;Component&nbsp;}&nbsp;from&nbsp;'react';\nimport&nbsp;Child&nbsp;from&nbsp;'.\/Child';\nclass&nbsp;Parent&nbsp;extends&nbsp;Component&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;childFunction;\n  \n&nbsp;&nbsp;&nbsp;&nbsp;setChildFunction&nbsp;=&nbsp;functionRef&nbsp;=&gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.childFunction&nbsp;=&nbsp;functionRef;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n    \n&nbsp;&nbsp;&nbsp;&nbsp;render()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(\n          &lt;div&gt;\n             &lt;Child setChildFunction={this.setChildFunction}&gt;&lt;\/Child&gt;\n            &lt;button onClick={this.childFunction}&gt;Trigger&nbsp;Child&lt;\/button&gt;\n          &lt;\/div&gt;\n        ); &nbsp;&nbsp;&nbsp;&nbsp;\n    } \n} \nexport&nbsp;default&nbsp;Parent;<\/pre><\/div>\n\n\n\n<p>First we created a variable called <em><strong>childFunction<\/strong> <\/em>that would hold the child function passed from <strong><em>Child<\/em><\/strong> component through <em><strong>setChildFunction<\/strong><\/em><strong><em>().<\/em><\/strong><br>Then we make a function name <em><strong>setChildFunction()<\/strong><\/em> which will pass to <em><strong>Child<\/strong> <\/em>as props.<br>This <em><strong>setChildFunction()<\/strong><\/em> receives a parameter (function) and set&nbsp; it to <em><strong>childFunction<\/strong> <\/em>variable which we created earlier.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror cm-s-default\" data-setting=\"{&quot;mode&quot;:&quot;jsx&quot;,&quot;mime&quot;:&quot;text\/jsx&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true}\">import&nbsp;React,&nbsp;{&nbsp;Component&nbsp;}&nbsp;from&nbsp;'react';\nclass&nbsp;Child&nbsp;extends&nbsp;Component&nbsp;{\n  \n&nbsp;&nbsp;&nbsp;&nbsp;triggerChild&nbsp;=&nbsp;()&nbsp;=&gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(\"child&nbsp;triggered&nbsp;from&nbsp;parent\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n    \n&nbsp;&nbsp;&nbsp;&nbsp;componentDidMount()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;{setChildFunction}&nbsp;=&nbsp;this.props;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setChildFunction(this.triggerChild);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;render()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(\n          &lt;div&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n            &lt;div&gt;Child&lt;\/div&gt;\n          &lt;\/div&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\nexport&nbsp;default&nbsp;Child;<\/pre><\/div>\n\n\n\n<p>In this <em><strong>Child<\/strong> <\/em>component, We passed the triggerChild function to Parent through setChildFunction when the component is rendered.&nbsp; That&#8217;s why inside componentDidMount() we grabbed the setChildFunction from props and invoked this function with this.triggerChild parameter which set this <em><strong>triggerChild<\/strong> <\/em>function to our parent variable (<em><strong>childFunction).<\/strong><\/em> Now, when we nee<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-performance-issues\">Performance Issues:<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>If any <strong><em>async<\/em><\/strong> function that will update the state of the component is called anywhere in the component then it must be unsubscribed or destroyed inside <strong><em>componentWillUnmount()<\/em><\/strong> function before the component unmounts.  <br><br><\/li><li>It&#8217;s better to use invoke any <strong><em>async<\/em><\/strong> function inside <strong><em>componentDidMount()<\/em><\/strong>. Because, if async function updates state before the component mounts, this will create an error, that is not in your expectation.<br><br><\/li><li>If you want to render a list, you can use a unique <strong><em>key <\/em><\/strong>for each item in the list. Here you might want to use <strong><em>index<\/em><\/strong> as <strong><em>key<\/em><\/strong> from <strong><em>map<\/em><\/strong> of a list. But this will cause performance issues when you want to delete or edit certain items in that list. You can use <strong>shortId<\/strong> for generating the short unique keys.<br><br><\/li><li>When rendering a long list it&#8217;s better to use an infinite scroll or load more mechanisms to increase performance. Because they render items based on user interaction.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Our React Products<\/h2>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-ui-lib wp-block-embed-ui-lib\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/ui-lib.com\/downloads\/bazar-pro-react-nextjs-ecommerce-template\/\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-ui-lib wp-block-embed-ui-lib\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Gv2iDG40rB\"><a href=\"https:\/\/ui-lib.com\/downloads\/matx-pro-react-admin\/\">MatX Pro &#8211; React Admin Template<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;MatX Pro &#8211; React Admin Template&#8221; &#8212; Ui Lib\" src=\"https:\/\/ui-lib.com\/downloads\/matx-pro-react-admin\/embed\/#?secret=zpFl6ZIICf#?secret=Gv2iDG40rB\" data-secret=\"Gv2iDG40rB\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-ui-lib wp-block-embed-ui-lib\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"xD1FARmiBd\"><a href=\"https:\/\/ui-lib.com\/downloads\/matx-free-react-admin-template\/\">MatX &#8211; Free React Admin Template<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;MatX &#8211; Free React Admin Template&#8221; &#8212; Ui Lib\" src=\"https:\/\/ui-lib.com\/downloads\/matx-free-react-admin-template\/embed\/#?secret=47ZEsM8UXg#?secret=xD1FARmiBd\" data-secret=\"xD1FARmiBd\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><strong><a href=\"https:\/\/1.envato.market\/Xx92Dg\" target=\"_blank\" rel=\"noopener\">Gull &#8211; React Bootstrap 5 Admin Dashboard Template<\/a><\/strong><\/p>\n\n\n\n<p>Learn more about us <a href=\"https:\/\/ui-lib.com\/\"><strong>HERE<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you a React developer looking forward to knowing about React Techniques with Performance. Don&#8217;t worry. Here, we are going to give you exactly what&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/ui-lib.com\/blog\/react-techniques-with-performance\/\">Continue reading<span class=\"screen-reader-text\">React Techniques with Performance<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":8623,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[141],"tags":[747,754,755,8,749,748,752,750,751,440,753,756],"class_list":["post-8585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","tag-browser-performance","tag-childfunction","tag-childfunction-from-parent","tag-react","tag-react-browser-performace","tag-react-performance","tag-react-performance-issues","tag-react-techniques","tag-react-techniques-with-performance","tag-reactjs-2","tag-setchildfunction","tag-triggerchild","entry"],"_links":{"self":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts\/8585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/comments?post=8585"}],"version-history":[{"count":4,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts\/8585\/revisions"}],"predecessor-version":[{"id":12838,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/posts\/8585\/revisions\/12838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/media\/8623"}],"wp:attachment":[{"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/media?parent=8585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/categories?post=8585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ui-lib.com\/blog\/wp-json\/wp\/v2\/tags?post=8585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}