

{"id":113099,"date":"2023-05-13T09:00:04","date_gmt":"2023-05-13T03:30:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=113099"},"modified":"2023-05-13T09:27:42","modified_gmt":"2023-05-13T03:57:42","slug":"react-memo","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/react-memo\/","title":{"rendered":"React Memo"},"content":{"rendered":"<p>React memo is a high-order component that allows you to optimize the performance of your React components by memoizing them. It helps prevent the re-rendering of a component when its props have not changed.<\/p>\n<h3>Key Benefits of React Memo:<\/h3>\n<p>One of the key benefits of using memo is that it can significantly improve the performance of your React components.<\/p>\n<p>By memoizing your component, you can ensure that it only re-renders when its props have changed, reducing the number of unnecessary re-renders.<\/p>\n<p>Another benefit of using a memo is that it makes it easier to write efficient and performant code. By wrapping your component in a memo call, you can let React take care of the memoization process for you.<\/p>\n<p>A memo is particularly useful for functional components, which are stateless and rely solely on their props for rendering. When you use memo with a functional component, you can ensure that the component only re-renders when its props have changed.<\/p>\n<p>To use memo, you simply need to wrap your component in a call to memo, passing it as a parameter. You can also provide a second parameter, which is a comparison function that will be used to determine whether the component should re-render or not.<\/p>\n<h3>Live Example: Simple Counter Component in react<\/h3>\n<p>Let&#8217;s take a look at an example to help you better understand how to use memo in React:<br \/>\nSay we have a simple counter component that takes in a count prop and displays the value on the screen:<\/p>\n<h4>Code-1: Re-renders every time:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import React from 'react';\r\n\r\nconst Counter = ({ count }) =&gt; {\r\n  return &lt;h1&gt;Count: {count}&lt;\/h1&gt;;\r\n};\r\n\r\nexport default Counter;\r\n<\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/zero-count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113443\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/zero-count.png\" alt=\"zero count\" width=\"219\" height=\"112\" \/><\/a><\/p>\n<p><strong>Output after incrementing it thrice:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113444\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/count.png\" alt=\"count\" width=\"236\" height=\"121\" \/><\/a><\/p>\n<h4>Explanation:<\/h4>\n<p>This component will re-render every time the count prop changes, even if the actual value of the count hasn&#8217;t changed. This can lead to unnecessary re-renders and decreased performance.<\/p>\n<p>This code exports a functional component called Counter. The component accepts an object prop as an argument and returns a h1 element with the string &#8220;Count: &#8220;.<\/p>\n<p>Since this component doesn&#8217;t have any internal state, it&#8217;s a stateless component, also known as a functional component. Instead of using the class syntax, it uses a function to define the component. The function accepts props as an argument and returns JSX to render.<\/p>\n<p>To fix this, we can use memo to memoize the component:<\/p>\n<h4>Code-2: Renders only when needed.<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import React, { memo } from 'react';\r\n\r\nconst Counter = memo(({ count }) =&gt; {\r\n  return &lt;h1&gt;Count: {count}&lt;\/h1&gt;;\r\n});\r\n\r\nexport default Counter;\r\n<\/pre>\n<p><strong>Initial Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/zero-count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113443\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/zero-count.png\" alt=\"zero count\" width=\"219\" height=\"112\" \/><\/a><\/p>\n<p><strong>Output after incrementing it thrice:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113444\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/count.png\" alt=\"count\" width=\"236\" height=\"121\" \/><\/a><\/p>\n<h4>Explanation:<\/h4>\n<p>This code exports a memoized functional component named Counter. A memoized component is a component that remembers its previous rendering results, and will only update and re-render when its props change. The Counter component takes a single prop count and returns an h1 element displaying the current value of the count.<\/p>\n<p>The memo function is a higher-order component that wraps the Counter component. It is used to optimize the rendering of Counter by preventing unnecessary re-renders when its count prop has not changed. It does this by memoizing the previous result of rendering Counter and only re-rendering it if the new count value differs.<\/p>\n<p>The memo function is a way of optimizing React applications by reducing the number of times components are re-rendered. By wrapping a component with the memo function, the component will only re-render if its props have changed.<\/p>\n<h3>Conclusion:<\/h3>\n<p>In conclusion, React memo is a powerful and efficient tool for optimizing the performance of your React components. By memoizing your components, you can ensure that they only re-render when their props have changed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React memo is a high-order component that allows you to optimize the performance of your React components by memoizing them. It helps prevent the re-rendering of a component when its props have not changed.&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":113442,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27227],"tags":[27401],"class_list":["post-113099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-tutorials","tag-react-memo"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>React Memo - DataFlair<\/title>\n<meta name=\"description\" content=\"React memo is a powerful and efficient tool for optimizing the performance of your React components. Learn more about it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/react-memo\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Memo - DataFlair\" \/>\n<meta property=\"og:description\" content=\"React memo is a powerful and efficient tool for optimizing the performance of your React components. Learn more about it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/react-memo\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-13T03:30:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-13T03:57:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/react-memo.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React Memo - DataFlair","description":"React memo is a powerful and efficient tool for optimizing the performance of your React components. Learn more about it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/react-memo\/","og_locale":"en_US","og_type":"article","og_title":"React Memo - DataFlair","og_description":"React memo is a powerful and efficient tool for optimizing the performance of your React components. Learn more about it.","og_url":"https:\/\/data-flair.training\/blogs\/react-memo\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-05-13T03:30:04+00:00","article_modified_time":"2023-05-13T03:57:42+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/react-memo.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/react-memo\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"React Memo","datePublished":"2023-05-13T03:30:04+00:00","dateModified":"2023-05-13T03:57:42+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/react-memo\/"},"wordCount":565,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/react-memo.webp","keywords":["React Memo"],"articleSection":["React Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/react-memo\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/react-memo\/","url":"https:\/\/data-flair.training\/blogs\/react-memo\/","name":"React Memo - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/react-memo.webp","datePublished":"2023-05-13T03:30:04+00:00","dateModified":"2023-05-13T03:57:42+00:00","description":"React memo is a powerful and efficient tool for optimizing the performance of your React components. Learn more about it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/react-memo\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/react-memo.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/03\/react-memo.webp","width":1200,"height":628,"caption":"react memo"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/react-memo\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"React Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/react-tutorials\/"},{"@type":"ListItem","position":3,"name":"React Memo"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/113099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=113099"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/113099\/revisions"}],"predecessor-version":[{"id":113445,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/113099\/revisions\/113445"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/113442"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=113099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=113099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=113099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}