{"id":8130,"date":"2023-06-21T09:16:02","date_gmt":"2023-06-21T09:16:02","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8130"},"modified":"2024-06-27T10:38:41","modified_gmt":"2024-06-27T10:38:41","slug":"react-animated-fade-between-colors","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/react-animated-fade-between-colors","title":{"rendered":"React Animated Fade Between Colors"},"content":{"rendered":"<p>To switch between colors we need logic to handle that.<br \/>\n<strong>For example:-<\/strong><br \/>\nAt the top of your component, import the useState Hook, which helps to define a state.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import { useState } from \"react\";<\/pre>\n<p>Let\u2019s define a state with default color black<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const [bgColor, setBgColor] = useState(\u201cblack\u201d);<\/pre>\n<p>Now, we\u2019ll declare a div container<br \/>\nTransition property will give smooth effect while color changes<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;div style={{ backgroundColor:bgColor, transition: \"all .5s ease\" }} \r\n&gt;&lt;\/div&gt;\r\n<\/pre>\n<p>Clicking the respective button will change the container background colour<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;button onClick={()=&gt;{setBgColor(\u2018green\u2019)}}&gt;Green&lt;\/button\r\n&lt;button onClick={()=&gt;{setBgColor(red)}}&gt;Red&lt;\/button\r\n&lt;button onClick={()=&gt;{setBgColor(blue)}}&gt;Blue&lt;\/button\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To switch between colors we need logic to handle that. For example:- At the top of your component, import the useState Hook, which helps to define a state. import { useState } from &#8220;react&#8221;; Let\u2019s define a state with default color black const [bgColor, setBgColor] = useState(\u201cblack\u201d); Now, we\u2019ll declare a div container Transition property [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8130"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8130"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8130\/revisions"}],"predecessor-version":[{"id":8343,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8130\/revisions\/8343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8342"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}