{"componentChunkName":"component---src-templates-docs-js","path":"/docs/optimizing-performance.html","result":{"data":{"markdownRemark":{"html":"<div class=\"scary\">\n<blockquote>\n<p>These docs are old and won’t be updated. Go to <a href=\"https://react.dev/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">react.dev</a> for the new React docs.</p>\n<p>These new documentation pages teach modern React:</p>\n<ul>\n<li><a href=\"https://react.dev/reference/react/memo#skipping-re-rendering-when-props-are-unchanged\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">memo</code>: Skipping re-rendering when props are unchanged\n</a></li>\n</ul>\n</blockquote>\n</div>\n<p>Internally, React uses several clever techniques to minimize the number of costly DOM operations required to update the UI. For many applications, using React will lead to a fast user interface without doing much work to specifically optimize for performance. Nevertheless, there are several ways you can speed up your React application.</p>\n<h2 id=\"use-the-production-build\"><a href=\"#use-the-production-build\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Use the Production Build </h2>\n<p>If you’re benchmarking or experiencing performance problems in your React apps, make sure you’re testing with the minified production build.</p>\n<p>By default, React includes many helpful warnings. These warnings are very useful in development. However, they make React larger and slower so you should make sure to use the production version when you deploy the app.</p>\n<p>If you aren’t sure whether your build process is set up correctly, you can check it by installing <a href=\"https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">React Developer Tools for Chrome</a>. If you visit a site with React in production mode, the icon will have a dark background:</p>\n\n  <a class=\"gatsby-resp-image-link\" href=\"/static/d0f767f80866431ccdec18f200ca58f1/0a47e/devtools-prod.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n  \n  <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block;  max-width: 600px; margin-left: auto; margin-right: auto;\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 20.952380952380953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAt0lEQVQI143D3W6DIBgAUN//hXrZJku8g1FBsy2ZVORHMSYtfoBgjc2udtuTnGLf95zz9oZnfuaUQ4zg3OnygSpajOMYQvAAwQcAD/+Wv8viPbjHAwDu6z3ltOUtpdXY6bflRVmWlDKM8ffXzycmtKoYq+uaIYQYZRjhpmmuhJArOYuLiy7F1Xt/HIcQouA3LqXSWkspecuV0toYYwYp5TgMfS+V0naa+l7cWm6tNdp0XRdCnOf5BWg72y43Hp2qAAAAAElFTkSuQmCC'); background-size: cover; display: block;\">\n      <img class=\"gatsby-resp-image-image\" style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\" alt=\"React DevTools on a website with production version of React\" title=\"\" src=\"/static/d0f767f80866431ccdec18f200ca58f1/0a47e/devtools-prod.png\" srcset=\"/static/d0f767f80866431ccdec18f200ca58f1/65ed1/devtools-prod.png 210w,\n/static/d0f767f80866431ccdec18f200ca58f1/d10fb/devtools-prod.png 420w,\n/static/d0f767f80866431ccdec18f200ca58f1/0a47e/devtools-prod.png 600w\" sizes=\"(max-width: 600px) 100vw, 600px\">\n    </span>\n  </span>\n  \n  </a>\n    \n<p>If you visit a site with React in development mode, the icon will have a red background:</p>\n\n  <a class=\"gatsby-resp-image-link\" href=\"/static/e434ce2f7e64f63e597edf03f4465694/0a47e/devtools-dev.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n  \n  <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block;  max-width: 600px; margin-left: auto; margin-right: auto;\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 33.33333333333333%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAABNElEQVQY023QyXKCQBSFYd7/pbKJA2jTgGAqRksUQjsgDfZ0ewBSIeUu3/JfnTqem1hrnbPuP/2Lc71zDgDqR3N6fyv3O09O1ERIIaUUQnAhxKsDgFISAIyRfW+HYdBas6qg9d3DIQ78IEk2aZr5vp+laRwnCK2DIIgwDjHGURQitFqh4xdum4sxVkoxDINzzjufC1JVZVHk+ak4F4RcLpcrIdVvLMuiLAkh1+s1z49FWVWEVNX3YX/QoK21Hmj9mi2lEkJyAKGUVCA1KFCKc66U0lorUIwxBTCOo7UWALz5bI6jaJPEm+hjOcNrP4nQFq23abxLo0/fD5aLRYhwiLAfBFm2fTI2TowxHqW0656csY7ypua05vWN1bcnffDmztu25YzRhjZN03WdMWYcR+fc320/DNOCC4EUU1UAAAAASUVORK5CYII='); background-size: cover; display: block;\">\n      <img class=\"gatsby-resp-image-image\" style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\" alt=\"React DevTools on a website with development version of React\" title=\"\" src=\"/static/e434ce2f7e64f63e597edf03f4465694/0a47e/devtools-dev.png\" srcset=\"/static/e434ce2f7e64f63e597edf03f4465694/65ed1/devtools-dev.png 210w,\n/static/e434ce2f7e64f63e597edf03f4465694/d10fb/devtools-dev.png 420w,\n/static/e434ce2f7e64f63e597edf03f4465694/0a47e/devtools-dev.png 600w\" sizes=\"(max-width: 600px) 100vw, 600px\">\n    </span>\n  </span>\n  \n  </a>\n    \n<p>It is expected that you use the development mode when working on your app, and the production mode when deploying your app to the users.</p>\n<p>You can find instructions for building your app for production below.</p>\n<h3 id=\"create-react-app\"><a href=\"#create-react-app\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create React App </h3>\n<p>If your project is built with <a href=\"https://github.com/facebookincubator/create-react-app\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Create React App</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\">npm run build</code></pre></div>\n<p>This will create a production build of your app in the <code class=\"gatsby-code-text\">build/</code> folder of your project.</p>\n<p>Remember that this is only necessary before deploying to production. For normal development, use <code class=\"gatsby-code-text\">npm start</code>.</p>\n<h3 id=\"single-file-builds\"><a href=\"#single-file-builds\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Single-File Builds </h3>\n<p>We offer production-ready versions of React and React DOM as single files:</p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre class=\"gatsby-code-html\"><code class=\"gatsby-code-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>https://unpkg.com/react@18/umd/react.production.min.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span>\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>https://unpkg.com/react-dom@18/umd/react-dom.production.min.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>script</span><span class=\"token punctuation\">></span></span></code></pre></div>\n<p>Remember that only React files ending with <code class=\"gatsby-code-text\">.production.min.js</code> are suitable for production.</p>\n<h3 id=\"brunch\"><a href=\"#brunch\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Brunch </h3>\n<p>For the most efficient Brunch production build, install the <a href=\"https://github.com/brunch/terser-brunch\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">terser-brunch</code></a> plugin:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\"># If you use npm\nnpm install --save-dev terser-brunch\n\n# If you use Yarn\nyarn add --dev terser-brunch</code></pre></div>\n<p>Then, to create a production build, add the <code class=\"gatsby-code-text\">-p</code> flag to the <code class=\"gatsby-code-text\">build</code> command:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\">brunch build -p</code></pre></div>\n<p>Remember that you only need to do this for production builds. You shouldn’t pass the <code class=\"gatsby-code-text\">-p</code> flag or apply this plugin in development, because it will hide useful React warnings and make the builds much slower.</p>\n<h3 id=\"browserify\"><a href=\"#browserify\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Browserify </h3>\n<p>For the most efficient Browserify production build, install a few plugins:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\"># If you use npm\nnpm install --save-dev envify terser uglifyify\n\n# If you use Yarn\nyarn add --dev envify terser uglifyify</code></pre></div>\n<p>To create a production build, make sure that you add these transforms <strong>(the order matters)</strong>:</p>\n<ul>\n<li>The <a href=\"https://github.com/hughsk/envify\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">envify</code></a> transform ensures the right build environment is set. Make it global (<code class=\"gatsby-code-text\">-g</code>).</li>\n<li>The <a href=\"https://github.com/hughsk/uglifyify\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">uglifyify</code></a> transform removes development imports. Make it global too (<code class=\"gatsby-code-text\">-g</code>).</li>\n<li>Finally, the resulting bundle is piped to <a href=\"https://github.com/terser-js/terser\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">terser</code></a> for mangling (<a href=\"https://github.com/hughsk/uglifyify#motivationusage\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">read why</a>).</li>\n</ul>\n<p>For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\">browserify ./index.js \\\n  -g [ envify --NODE_ENV production ] \\\n  -g uglifyify \\\n  | terser --compress --mangle > ./bundle.js</code></pre></div>\n<p>Remember that you only need to do this for production builds. You shouldn’t apply these plugins in development because they will hide useful React warnings, and make the builds much slower.</p>\n<h3 id=\"rollup\"><a href=\"#rollup\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Rollup </h3>\n<p>For the most efficient Rollup production build, install a few plugins:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token comment\"># If you use npm</span>\n<span class=\"token function\">npm</span> <span class=\"token function\">install</span> --save-dev rollup-plugin-commonjs rollup-plugin-replace rollup-plugin-terser\n\n<span class=\"token comment\"># If you use Yarn</span>\n<span class=\"token function\">yarn</span> <span class=\"token function\">add</span> <span class=\"token parameter variable\">--dev</span> rollup-plugin-commonjs rollup-plugin-replace rollup-plugin-terser</code></pre></div>\n<p>To create a production build, make sure that you add these plugins <strong>(the order matters)</strong>:</p>\n<ul>\n<li>The <a href=\"https://github.com/rollup/rollup-plugin-replace\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">replace</code></a> plugin ensures the right build environment is set.</li>\n<li>The <a href=\"https://github.com/rollup/rollup-plugin-commonjs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">commonjs</code></a> plugin provides support for CommonJS in Rollup.</li>\n<li>The <a href=\"https://github.com/TrySound/rollup-plugin-terser\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">terser</code></a> plugin compresses and mangles the final bundle.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token literal-property property\">plugins</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n  <span class=\"token comment\">// ...</span>\n  <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'rollup-plugin-replace'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token string-property property\">'process.env.NODE_ENV'</span><span class=\"token operator\">:</span> <span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span><span class=\"token string\">'production'</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n  <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'rollup-plugin-commonjs'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n  <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'rollup-plugin-terser'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n  <span class=\"token comment\">// ...</span>\n<span class=\"token punctuation\">]</span></code></pre></div>\n<p>For a complete setup example <a href=\"https://gist.github.com/Rich-Harris/cb14f4bc0670c47d00d191565be36bf0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">see this gist</a>.</p>\n<p>Remember that you only need to do this for production builds. You shouldn’t apply the <code class=\"gatsby-code-text\">terser</code> plugin or the <code class=\"gatsby-code-text\">replace</code> plugin with <code class=\"gatsby-code-text\">'production'</code> value in development because they will hide useful React warnings, and make the builds much slower.</p>\n<h3 id=\"webpack\"><a href=\"#webpack\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>webpack </h3>\n<blockquote>\n<p><strong>Note:</strong></p>\n<p>If you’re using Create React App, please follow <a href=\"#create-react-app\">the instructions above</a>.<br>\nThis section is only relevant if you configure webpack directly.</p>\n</blockquote>\n<p>Webpack v4+ will minify your code by default in production mode.</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">const</span> TerserPlugin <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'terser-webpack-plugin'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\nmodule<span class=\"token punctuation\">.</span>exports <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token literal-property property\">mode</span><span class=\"token operator\">:</span> <span class=\"token string\">'production'</span><span class=\"token punctuation\">,</span>\n  <span class=\"token literal-property property\">optimization</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">minimizer</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">TerserPlugin</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> <span class=\"token comment\">/* additional options here */</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>You can learn more about this in <a href=\"https://webpack.js.org/guides/production/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">webpack documentation</a>.</p>\n<p>Remember that you only need to do this for production builds. You shouldn’t apply <code class=\"gatsby-code-text\">TerserPlugin</code> in development because it will hide useful React warnings, and make the builds much slower.</p>\n<h2 id=\"profiling-components-with-the-devtools-profiler\"><a href=\"#profiling-components-with-the-devtools-profiler\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Profiling Components with the DevTools Profiler </h2>\n<p><code class=\"gatsby-code-text\">react-dom</code> 16.5+ and <code class=\"gatsby-code-text\">react-native</code> 0.57+ provide enhanced profiling capabilities in DEV mode with the React DevTools Profiler.\nAn overview of the Profiler can be found in the blog post <a href=\"/blog/2018/09/10/introducing-the-react-profiler.html\">“Introducing the React Profiler”</a>.\nA video walkthrough of the profiler is also <a href=\"https://www.youtube.com/watch?v=nySib7ipZdk\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">available on YouTube</a>.</p>\n<p>If you haven’t yet installed the React DevTools, you can find them here:</p>\n<ul>\n<li><a href=\"https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Chrome Browser Extension</a></li>\n<li><a href=\"https://addons.mozilla.org/en-GB/firefox/addon/react-devtools/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Firefox Browser Extension</a></li>\n<li><a href=\"https://www.npmjs.com/package/react-devtools\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Standalone Node Package</a></li>\n</ul>\n<blockquote>\n<p>Note</p>\n<p>A production profiling bundle of <code class=\"gatsby-code-text\">react-dom</code> is also available as <code class=\"gatsby-code-text\">react-dom/profiling</code>.\nRead more about how to use this bundle at <a href=\"https://fb.me/react-profiling\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">fb.me/react-profiling</a></p>\n</blockquote>\n<blockquote>\n<p>Note</p>\n<p>Before React 17, we use the standard <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">User Timing API</a> to profile components with the chrome performance tab.\nFor a more detailed walkthrough, check out <a href=\"https://calibreapp.com/blog/react-performance-profiling-optimization\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this article by Ben Schwarz</a>.</p>\n</blockquote>\n<h2 id=\"virtualize-long-lists\"><a href=\"#virtualize-long-lists\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Virtualize Long Lists </h2>\n<p>If your application renders long lists of data (hundreds or thousands of rows), we recommend using a technique known as “windowing”. This technique only renders a small subset of your rows at any given time, and can dramatically reduce the time it takes to re-render the components as well as the number of DOM nodes created.</p>\n<p><a href=\"https://react-window.now.sh/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">react-window</a> and <a href=\"https://bvaughn.github.io/react-virtualized/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">react-virtualized</a> are popular windowing libraries. They provide several reusable components for displaying lists, grids, and tabular data. You can also create your own windowing component, like <a href=\"https://medium.com/@paularmstrong/twitter-lite-and-high-performance-react-progressive-web-apps-at-scale-d28a00e780a3\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Twitter did</a>, if you want something more tailored to your application’s specific use case.</p>\n<h2 id=\"avoid-reconciliation\"><a href=\"#avoid-reconciliation\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Avoid Reconciliation </h2>\n<p>React builds and maintains an internal representation of the rendered UI. It includes the React elements you return from your components. This representation lets React avoid creating DOM nodes and accessing existing ones beyond necessity, as that can be slower than operations on JavaScript objects. Sometimes it is referred to as a “virtual DOM”, but it works the same way on React Native.</p>\n<p>When a component’s props or state change, React decides whether an actual DOM update is necessary by comparing the newly returned element with the previously rendered one. When they are not equal, React will update the DOM.</p>\n<p>Even though React only updates the changed DOM nodes, re-rendering still takes some time. In many cases it’s not a problem, but if the slowdown is noticeable, you can speed all of this up by overriding the lifecycle function <code class=\"gatsby-code-text\">shouldComponentUpdate</code>, which is triggered before the re-rendering process starts. The default implementation of this function returns <code class=\"gatsby-code-text\">true</code>, leaving React to perform the update:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">shouldComponentUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">nextProps<span class=\"token punctuation\">,</span> nextState</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>If you know that in some situations your component doesn’t need to update, you can return <code class=\"gatsby-code-text\">false</code> from <code class=\"gatsby-code-text\">shouldComponentUpdate</code> instead, to skip the whole rendering process, including calling <code class=\"gatsby-code-text\">render()</code> on this component and below.</p>\n<p>In most cases, instead of writing <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> by hand, you can inherit from <a href=\"/docs/react-api.html#reactpurecomponent\"><code class=\"gatsby-code-text\">React.PureComponent</code></a>. It is equivalent to implementing <code class=\"gatsby-code-text\">shouldComponentUpdate()</code> with a shallow comparison of current and previous props and state.</p>\n<h2 id=\"shouldcomponentupdate-in-action\"><a href=\"#shouldcomponentupdate-in-action\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>shouldComponentUpdate In Action </h2>\n<p>Here’s a subtree of components. For each one, <code class=\"gatsby-code-text\">SCU</code> indicates what <code class=\"gatsby-code-text\">shouldComponentUpdate</code> returned, and <code class=\"gatsby-code-text\">vDOMEq</code> indicates whether the rendered React elements were equivalent. Finally, the circle’s color indicates whether the component had to be reconciled or not.</p>\n<figure>\n  <a class=\"gatsby-resp-image-link\" href=\"/static/5ee1bdf4779af06072a17b7a0654f6db/cd039/should-component-update.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n  \n  <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block;  max-width: 555px; margin-left: auto; margin-right: auto;\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 66.66666666666666%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADOklEQVQ4y1WTYWxTVRiGn3t72+12ZbZrCyvWMMHEiTMYCcZosiFK9BcqQsSkQafBkZkxZ+ZgW40drLNLYW50sG4lQLt1tkW3IKGMLLhYWKdzI7KAaAKYKLhgjAn/TPxx9NyB0y/5cs/9znve7z3nvAe3145npYut772AalKQsa1iNRWgzewLdOXefv3VC1AmnqjULgD1ikqw+mme+hgQ8GQdbGhnMRoGtrPdX8nJK0mTEEKWFLNZKwQs4kgUo2b3mgFNjmX+Eu0zzaPzDTAF/Aycv0fYmqyhKbqZz+eOLSz2uiWZKudEakgVf01TDVK62eOwWyXm5qEIt7EyA1wG5v9LeFGMcEvElKNfRjoyH/gqEYJNQB3g8/koKSkBRTGaXQqFONfSsnZ6f3tH4CUemQWb3NMZrwnxEGgUQzofUzsLBIP5iP/c4L7H24QgKH5XJPD2jXGD6EZnUBWZEfWPdIJsQ92aXMAfOm3XH56BpbXAZ6NtKjWgBIDEVDf3zkb8miadO8Dw7BDxmWPMXz2DmBjjWmdw4TheqbII8Sfit/OI8uWwuswi64nJg7ALVEkYn+jtGs71l09cP0lXptUZn+iNZOcyrlNXUnyViapz0RiX9ocDyabXdvrBnhrrWTd8tqf7TmGBc/TaKIdPhddHz4a6ihso0zucOsmp3sbsd+lVYz+m6P96r57M9+4enRxy5X7KcnHkiPrDQB9Xw6Ed37c0bbwJSxInmh89kT/8/qzLe1/28nGS0z0Vg5ORemrxmAOah/RcjE8+DRoX9NzWZwin/Gx5c5PxL7eTO9CofLt3D+PvvkwrUNu4me6knzqbzcB0pptJ5A8at6h9pMLp6+m2mubqBwG71aa7Sh9wuzVNWwrocsGHe+zsrllR1Lij8H4HOCy6pXTZcucyM0hMqavUIa2k8CJobSYYn43H+46HVgAmp9thBaQP5VeamfA6aF9D0c6NVPAWxRoUoBnNrHdTY8GnC3HnUPSLgV315VKhrVCXXV2AU875qqD6efBtwPLOs1S9UcljgM1sMjAS65YqgSX/Ek7Huj2Zo/1SjVLisCt3X4nRUSym9GWRkOoFrF98ucr/1P0TfwMynTKkr7evxAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\">\n      <img class=\"gatsby-resp-image-image\" style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\" alt=\"should component update\" title=\"\" src=\"/static/5ee1bdf4779af06072a17b7a0654f6db/cd039/should-component-update.png\" srcset=\"/static/5ee1bdf4779af06072a17b7a0654f6db/65ed1/should-component-update.png 210w,\n/static/5ee1bdf4779af06072a17b7a0654f6db/d10fb/should-component-update.png 420w,\n/static/5ee1bdf4779af06072a17b7a0654f6db/cd039/should-component-update.png 555w\" sizes=\"(max-width: 555px) 100vw, 555px\">\n    </span>\n  </span>\n  \n  </a>\n    </figure>\n<p>Since <code class=\"gatsby-code-text\">shouldComponentUpdate</code> returned <code class=\"gatsby-code-text\">false</code> for the subtree rooted at C2, React did not attempt to render C2, and thus didn’t even have to invoke <code class=\"gatsby-code-text\">shouldComponentUpdate</code> on C4 and C5.</p>\n<p>For C1 and C3, <code class=\"gatsby-code-text\">shouldComponentUpdate</code> returned <code class=\"gatsby-code-text\">true</code>, so React had to go down to the leaves and check them. For C6 <code class=\"gatsby-code-text\">shouldComponentUpdate</code> returned <code class=\"gatsby-code-text\">true</code>, and since the rendered elements weren’t equivalent React had to update the DOM.</p>\n<p>The last interesting case is C8. React had to render this component, but since the React elements it returned were equal to the previously rendered ones, it didn’t have to update the DOM.</p>\n<p>Note that React only had to do DOM mutations for C6, which was inevitable. For C8, it bailed out by comparing the rendered React elements, and for C2’s subtree and C7, it didn’t even have to compare the elements as we bailed out on <code class=\"gatsby-code-text\">shouldComponentUpdate</code>, and <code class=\"gatsby-code-text\">render</code> was not called.</p>\n<h2 id=\"examples\"><a href=\"#examples\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Examples </h2>\n<p>If the only way your component ever changes is when the <code class=\"gatsby-code-text\">props.color</code> or the <code class=\"gatsby-code-text\">state.count</code> variable changes, you could have <code class=\"gatsby-code-text\">shouldComponentUpdate</code> check that:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">CounterButton</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token literal-property property\">count</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">shouldComponentUpdate</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">nextProps<span class=\"token punctuation\">,</span> nextState</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>color <span class=\"token operator\">!==</span> nextProps<span class=\"token punctuation\">.</span>color<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>count <span class=\"token operator\">!==</span> nextState<span class=\"token punctuation\">.</span>count<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token keyword\">return</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span>\n        <span class=\"token attr-name\">color</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>color<span class=\"token punctuation\">}</span></span>\n        <span class=\"token attr-name\">onClick</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">state</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">count</span><span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>count <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n        Count: </span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>count<span class=\"token punctuation\">}</span><span class=\"token plain-text\">\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>button</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>In this code, <code class=\"gatsby-code-text\">shouldComponentUpdate</code> is just checking if there is any change in <code class=\"gatsby-code-text\">props.color</code> or <code class=\"gatsby-code-text\">state.count</code>. If those values don’t change, the component doesn’t update. If your component got more complex, you could use a similar pattern of doing a “shallow comparison” between all the fields of <code class=\"gatsby-code-text\">props</code> and <code class=\"gatsby-code-text\">state</code> to determine if the component should update. This pattern is common enough that React provides a helper to use this logic - just inherit from <code class=\"gatsby-code-text\">React.PureComponent</code>. So this code is a simpler way to achieve the same thing:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">CounterButton</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>PureComponent</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token literal-property property\">count</span><span class=\"token operator\">:</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span>\n        <span class=\"token attr-name\">color</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>color<span class=\"token punctuation\">}</span></span>\n        <span class=\"token attr-name\">onClick</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">state</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">count</span><span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>count <span class=\"token operator\">+</span> <span class=\"token number\">1</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n        Count: </span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>count<span class=\"token punctuation\">}</span><span class=\"token plain-text\">\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>button</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Most of the time, you can use <code class=\"gatsby-code-text\">React.PureComponent</code> instead of writing your own <code class=\"gatsby-code-text\">shouldComponentUpdate</code>. It only does a shallow comparison, so you can’t use it if the props or state may have been mutated in a way that a shallow comparison would miss.</p>\n<p>This can be a problem with more complex data structures. For example, let’s say you want a <code class=\"gatsby-code-text\">ListOfWords</code> component to render a comma-separated list of words, with a parent <code class=\"gatsby-code-text\">WordAdder</code> component that lets you click a button to add a word to the list. This code does <em>not</em> work correctly:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">ListOfWords</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>PureComponent</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>words<span class=\"token punctuation\">.</span><span class=\"token function\">join</span><span class=\"token punctuation\">(</span><span class=\"token string\">','</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">WordAdder</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">React<span class=\"token punctuation\">.</span>Component</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">props</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">super</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">words</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'marklar'</span><span class=\"token punctuation\">]</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>handleClick <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">handleClick</span><span class=\"token punctuation\">.</span><span class=\"token function\">bind</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">handleClick</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// This section is bad style and causes a bug</span>\n    <span class=\"token keyword\">const</span> words <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>words<span class=\"token punctuation\">;</span>\n    words<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span><span class=\"token string\">'marklar'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token literal-property property\">words</span><span class=\"token operator\">:</span> words<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">render</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n      <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n        </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>button</span> <span class=\"token attr-name\">onClick</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>handleClick<span class=\"token punctuation\">}</span></span> <span class=\"token punctuation\">/></span></span><span class=\"token plain-text\">\n        </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">ListOfWords</span></span> <span class=\"token attr-name\">words</span><span class=\"token script language-javascript\"><span class=\"token script-punctuation punctuation\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>state<span class=\"token punctuation\">.</span>words<span class=\"token punctuation\">}</span></span> <span class=\"token punctuation\">/></span></span><span class=\"token plain-text\">\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The problem is that <code class=\"gatsby-code-text\">PureComponent</code> will do a simple comparison between the old and new values of <code class=\"gatsby-code-text\">this.props.words</code>. Since this code mutates the <code class=\"gatsby-code-text\">words</code> array in the <code class=\"gatsby-code-text\">handleClick</code> method of <code class=\"gatsby-code-text\">WordAdder</code>, the old and new values of <code class=\"gatsby-code-text\">this.props.words</code> will compare as equal, even though the actual words in the array have changed. The <code class=\"gatsby-code-text\">ListOfWords</code> will thus not update even though it has new words that should be rendered.</p>\n<h2 id=\"the-power-of-not-mutating-data\"><a href=\"#the-power-of-not-mutating-data\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Power Of Not Mutating Data </h2>\n<p>The simplest way to avoid this problem is to avoid mutating values that you are using as props or state. For example, the <code class=\"gatsby-code-text\">handleClick</code> method above could be rewritten using <code class=\"gatsby-code-text\">concat</code> as:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">handleClick</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">state</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">words</span><span class=\"token operator\">:</span> state<span class=\"token punctuation\">.</span>words<span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token string\">'marklar'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>ES6 supports a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">spread syntax</a> for arrays which can make this easier. If you’re using Create React App, this syntax is available by default.</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token function\">handleClick</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token function\">setState</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">state</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">words</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token operator\">...</span>state<span class=\"token punctuation\">.</span>words<span class=\"token punctuation\">,</span> <span class=\"token string\">'marklar'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>You can also rewrite code that mutates objects to avoid mutation, in a similar way. For example, let’s say we have an object named <code class=\"gatsby-code-text\">colormap</code> and we want to write a function that changes <code class=\"gatsby-code-text\">colormap.right</code> to be <code class=\"gatsby-code-text\">'blue'</code>. We could write:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">function</span> <span class=\"token function\">updateColorMap</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">colormap</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  colormap<span class=\"token punctuation\">.</span>right <span class=\"token operator\">=</span> <span class=\"token string\">'blue'</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>To write this without mutating the original object, we can use <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Object.assign</a> method:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">function</span> <span class=\"token function\">updateColorMap</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">colormap</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> Object<span class=\"token punctuation\">.</span><span class=\"token function\">assign</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> colormap<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span><span class=\"token literal-property property\">right</span><span class=\"token operator\">:</span> <span class=\"token string\">'blue'</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><code class=\"gatsby-code-text\">updateColorMap</code> now returns a new object, rather than mutating the old one. <code class=\"gatsby-code-text\">Object.assign</code> is in ES6 and requires a polyfill.</p>\n<p><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Object spread syntax</a> makes it easier to update objects without mutation as well:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">function</span> <span class=\"token function\">updateColorMap</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">colormap</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span>colormap<span class=\"token punctuation\">,</span> <span class=\"token literal-property property\">right</span><span class=\"token operator\">:</span> <span class=\"token string\">'blue'</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This feature was added to JavaScript in ES2018.</p>\n<p>If you’re using Create React App, both <code class=\"gatsby-code-text\">Object.assign</code> and the object spread syntax are available by default.</p>\n<p>When you deal with deeply nested objects, updating them in an immutable way can feel convoluted. If you run into this problem, check out <a href=\"https://github.com/mweststrate/immer\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Immer</a> or <a href=\"https://github.com/kolodny/immutability-helper\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">immutability-helper</a>. These libraries let you write highly readable code without losing the benefits of immutability.</p>","frontmatter":{"title":"Optimizing Performance","next":null,"prev":null},"fields":{"path":"content/docs/optimizing-performance.md","slug":"docs/optimizing-performance.html"}}},"pageContext":{"slug":"docs/optimizing-performance.html"}},"staticQueryHashes":[]}