Building a Background Color Changing App with React

Hey there! I'm Shivam, a beginner web developer who's passionate about coding and creating beautiful websites. I'm currently in the process of learning everything I can about web development, and I'm always excited to discover new tools and techniques that can help me grow as a developer.
This blog is where I'll be sharing my journey and what I'm learning along the way. From tips and tricks to code snippets, I hope to help other beginners like me as we navigate the world of web development together.
Thanks for stopping by, and I hope you'll join me on this journey as we learn and grow together!
Introduction
In this article, we'll explore how to create a simple background color changing app using React. We'll walk through the code step by step, explaining each component and concept involved. By the end, you'll have a clear understanding of how to utilize React state and event handling to build interactive user interfaces.
Setting up the Project
Before diving into the code, ensure you have Node.js and npm installed on your system. We'll use Create React App to bootstrap our project, making the setup process quick and easy. Run the following command in your terminal:
npx create-react-app color-changer
cd color-changer
Once the project is set up, navigate to the src directory and open the App.js file.
Understanding the Code
Let's break down the key components and functionalities of our background color changer app:
- useState Hook: We import the
useStatehook from React, which allows us to add state variables to functional components. In ourAppcomponent, we useuseStateto manage thecolorstate, initializing it with the color "olive".
import { useState } from "react";
function App() {
const [color, setColor] = useState("olive");
// Other code...
}
- Color Array: We define an array
colorscontaining various color options that users can choose from to change the background color dynamically.
const colors = ["red", "green","blue","olive","gray","yellow","pink","purple","lavender","white","black"];
- Rendering UI: Inside the
returnstatement, we render the UI elements. We set the background color of the entire page using thestyleattribute bound to thecolorstate.
return (
<>
<div
className="w-full h-screen duration-200 "
style={{ backgroundColor: color }}
>
{/* Other code... */}
</div>
</>
);
- Color Buttons: We map through the
colorsarray to render color selection buttons. Each button triggers thesetColorfunction with the corresponding color when clicked.
{colors.map((color) => (
<button
key={color}
onClick={() => setColor(color)}
className="outline-none px-4 py-1 rounded-full text-white shadow-lg "
style={{ backgroundColor: color }}
>
{color}
</button>
))}
Key Learnings:
Utilizing React hooks, such as
useState, enables us to manage state within functional components.Event handling in React components, like
onClick, allows us to define actions in response to user interactions.Leveraging dynamic rendering through array mapping enables scalable and maintainable UI components.
Conclusion
In this tutorial, we've built a simple yet interactive background color changer app using React. We've explored the fundamentals of React state management, event handling, and dynamic UI rendering. With these foundational concepts, you can expand the functionality of the app or apply similar techniques to other React projects. Happy coding!



