{"id":76943,"date":"2025-12-08T12:37:13","date_gmt":"2025-12-08T07:07:13","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=76943"},"modified":"2025-12-26T10:09:59","modified_gmt":"2025-12-26T04:39:59","slug":"conditional-rendering-in-react-19-activity-component","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/conditional-rendering-in-react-19-activity-component\/","title":{"rendered":"Activity Component: Conditional Rendering in React 19."},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Traditionally, we used ternary operators or CSS style attributes (display and visibility) to implement conditional rendering in a React component, but the issue with this approach was that either it caused internal state loss or was not performance-friendly. With React 19, a new component called <strong>Activity<\/strong> has been introduced which can implement conditional rendering while <strong>preserving and pausing<\/strong> the internal state of the component. Previously it was used to handle by either of the two approaches &#8211;<\/p>\n<ol>\n<li><strong>Ternary operators (?, &amp;&amp; )<\/strong><\/li>\n<\/ol>\n<div id=\"attachment_76988\" style=\"width: 643px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-76988\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-76988\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-164809.jpeg\" alt=\"Ternary operators\" width=\"633\" height=\"285\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-164809.jpeg 582w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-164809-300x135.jpeg 300w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><p id=\"caption-attachment-76988\" class=\"wp-caption-text\">Ternary operators to conditionally render components<\/p><\/div>\n<p>With ternary operators, the approach was clean and simple for dual component logic, but became messy if multiple components had to be switched. Also, the major drawback had been<strong> internal<\/strong> <strong>State Loss<\/strong><\/p>\n<p>2<strong>. CSS property<\/strong><\/p>\n<div id=\"attachment_76989\" style=\"width: 717px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-76989\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-76989\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-170747.jpeg\" alt=\"CSS style property\" width=\"707\" height=\"296\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-170747.jpeg 707w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-170747-300x126.jpeg 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Screenshot-2025-12-09-170747-624x261.jpeg 624w\" sizes=\"(max-width: 707px) 100vw, 707px\" \/><p id=\"caption-attachment-76989\" class=\"wp-caption-text\">Conditionally rendering via CSS style properties<\/p><\/div>\n<p>With CSS properties like display or visibility, it preserves state, but since the component is never truly paused, it might prove expensive to implement it for large component switching.<\/p>\n<h2>Activity Component Implementation and states<\/h2>\n<p>The Activity component solves both of these issues and provides a middle ground for implementing a performance-friendly conditional rendering approach. Using the Activity component is pretty simple; we just need to import it, and it uses a prop called <strong>&#8220;mode&#8221;<\/strong> which controls the visibility of the child content. It has two valid values \u2014 <strong>visible and hidden.<\/strong><\/p>\n<div id=\"attachment_76948\" style=\"width: 761px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-76948\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-76948\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Parent-com-2-300x188.png\" alt=\"Parent component with Activity Wrapper\" width=\"751\" height=\"471\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Parent-com-2-300x188.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Parent-com-2-624x390.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Parent-com-2.png 1084w\" sizes=\"(max-width: 751px) 100vw, 751px\" \/><p id=\"caption-attachment-76948\" class=\"wp-caption-text\">Parent component with Activity Wrapper<\/p><\/div>\n<p>&nbsp;<\/p>\n<h3>Visible value &#8211;<\/h3>\n<div id=\"attachment_76949\" style=\"width: 764px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-76949\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-76949\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/12\/show-300x199.png\" alt=\"Showing child with prop value - visible\" width=\"754\" height=\"500\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/12\/show-300x199.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/show-768x510.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/show-624x415.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/show.png 1020w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><p id=\"caption-attachment-76949\" class=\"wp-caption-text\">Showing child with prop value &#8211; visible<\/p><\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>is shown on the screen<\/li>\n<li>keeps its state<\/li>\n<li>effects (useEffect) run as normal<\/li>\n<li>DOM is present<\/li>\n<\/ul>\n<h3>Hidden value &#8211;<\/h3>\n<div id=\"attachment_76950\" style=\"width: 749px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-76950\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-76950\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1-300x125.png\" alt=\"Hiding Child component with prop passed as hidden in Activity wrapper\" width=\"739\" height=\"308\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1-300x125.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1-1024x427.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1-768x321.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1-1536x641.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1-624x260.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/child-1.png 1864w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><p id=\"caption-attachment-76950\" class=\"wp-caption-text\">Hiding Child component with prop passed as hidden in Activity wrapper<\/p><\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>becomes invisible (display: none)<\/li>\n<li>state is preserved (does NOT reset!)<\/li>\n<li>effects are paused \u2014 React runs cleanup<\/li>\n<li>on becoming visible again, effects re-run with the old state<\/li>\n<li>DOM is temporarily removed from layout (not destroyed)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div id=\"attachment_76951\" style=\"width: 755px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-76951\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-76951\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2-300x116.png\" alt=\"Restoring Child component\" width=\"745\" height=\"288\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2-300x116.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2-1024x395.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2-768x296.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2-1536x593.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2-624x241.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/12\/Child-2.png 1884w\" sizes=\"(max-width: 745px) 100vw, 745px\" \/><p id=\"caption-attachment-76951\" class=\"wp-caption-text\">Restoring Child component with prop visible. The count state is still preserved<\/p><\/div>\n<p>Even when the child component is hidden and brought back, we can see that the internal state of the component is persisted.<\/p>\n<p><strong>When to use what &#8211;\u00a0<\/strong><\/p>\n<table style=\"border-collapse: collapse; width: 100%; height: 120px;\">\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"width: 25%; text-align: center; height: 24px;\"><strong>Feature\u00a0<\/strong><\/td>\n<td style=\"width: 25%; text-align: center; height: 24px;\"><strong>Ternary<\/strong><\/td>\n<td style=\"width: 25%; text-align: center; height: 24px;\"><strong>CSS<\/strong><\/td>\n<td style=\"width: 25%; text-align: center; height: 24px;\"><strong>Activity<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 25%; height: 24px; text-align: center;\"><strong>Unmounts<\/strong><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #00ff00;\">Yes<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #ff0000;\">No<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #ff0000;\">No<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 25%; height: 24px; text-align: center;\"><strong>State Preserved<\/strong><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #ff0000;\">No<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #00ff00;\">Yes<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #00ff00;\">Yes<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 25%; height: 24px; text-align: center;\"><strong>Effects Paused<\/strong><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #ff0000;\">No<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #ff0000;\">No<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #00ff00;\">Yes<\/span><\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"width: 25%; height: 24px; text-align: center;\"><strong>Performance Friendly<\/strong><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #00ff00;\">Yes<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #ff0000;\">No<\/span><\/td>\n<td style=\"width: 25%; height: 24px; text-align: center;\"><span style=\"color: #00ff00;\">Yes<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><\/h2>\n<h2>Conclusion<\/h2>\n<p>Activity Component is a great way to implement features like multiple tabs, side panels, wizards, and much more with reduced lines of code that we can usually implement with traditional conditional rendering, and it improves performance since React effects are paused. Although it doesn\u2019t completely replace traditional conditional rendering, as certain scenarios require effects to rerun or transitions to be handled, it is a strong addition in React 19 for cases where preserving internal state with minimal code is essential.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Traditionally, we used ternary operators or CSS style attributes (display and visibility) to implement conditional rendering in a React component, but the issue with this approach was that either it caused internal state loss or was not performance-friendly. With React 19, a new component called Activity has been introduced which can implement conditional rendering [&hellip;]<\/p>\n","protected":false},"author":1581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":56},"categories":[5876],"tags":[8252,1308,477,4064],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/76943"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1581"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=76943"}],"version-history":[{"count":13,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/76943\/revisions"}],"predecessor-version":[{"id":77165,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/76943\/revisions\/77165"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=76943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=76943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=76943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}