Name | Progress |
---|---|
Course outline | Not Read |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: Pet Parade | Not Read |
Challenge 1 | Not Attempted |
Quiz 1 | Not Attempted |
Name | Progress |
---|---|
Project: Profile Card Creator | Not Read |
Key Concepts | Not Read |
Challenge 2 | Not Attempted |
Quiz 2 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: Weather Wardrobe | Not Read |
Challenge 4 | Not Attempted |
Quiz 4 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Interactive Quiz App" | Not Read |
Challenge 5 | Not Attempted |
Quiz 5 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Photo Gallery" | Not Read |
Challenge 6 | Not Attempted |
Quiz 6 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Registration Form" | Not Read |
Challenge 7 | Not Read |
Quiz 7 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Weather Dashboard" | Not Read |
Challenge 8 | Not Attempted |
Quiz 8 | Not Attempted |
In the "Magic Garden" project, you will build a virtual garden that evolves in real-time. Flowers in this garden will grow, bloom, and change colors over time, all managed through React's state management and effects. This project will help you understand how to apply <strong>useState</strong> and <strong>useEffect</strong> to create dynamic and interactive applications.
Step 1: Set Up Your Project
Ensure your React development environment is ready. If needed, set up a new project:
npm create vite@latest magic-garden -- --template react
cd magic-garden
npm install
npm run dev
Step 2: Create the Garden Component
This component will be the main area where your flowers are displayed and managed.
import React, { useState, useEffect } from 'react';
import Flower from './Flower';
function Garden() {
const [flowers, setFlowers] = useState([]);
useEffect(() => {
const interval = setInterval(() => {
const newFlowers = flowers.map(flower => ({
...flower,
size: flower.size * 1.1, // Flowers grow over time
}));
setFlowers(newFlowers);
}, 1000); // Update every second to grow flowers
return () => clearInterval(interval); // Clean up the interval on component unmount
}, [flowers]);
// Function to plant a new flower
const plantFlower = () => {
setFlowers([...flowers, { id: flowers.length, size: 1, color: 'pink' }]);
};
return (
<div>
<button onClick={plantFlower}>Plant a Flower</button>
<div className="garden">
{flowers.map(flower => (
<Flower key={flower.id} size={flower.size} color={flower.color} />
))}
</div>
</div>
);
}
export default Garden;
import React from 'react';
function Flower({ size, color }) {
return (
<div style={{
width: size,
height: size,
backgroundColor: color,
borderRadius: '50%',
}}></div>
);
}
export default Flower;
Step 3: Styling Your Garden
Add basic CSS to your application to make the garden and flowers look more appealing.
.garden {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
gap: 10px;
}
button {
margin: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
Step 4: Testing Your Garden
Run your application and test planting flowers. Observe how they grow in size over time automatically.
As you become more comfortable with state and effects, consider adding features like:
This project will help you solidify your understanding of React's state management and effects, providing a fun and interactive way to see these concepts in action. Enjoy building your magical garden and watch it come to life!