Project: Magic Garden
by Andrew Nguyenยท Module 03 - State and Effect

Introduction
Not Attempted
NameProgress
Course outline
Not Read
Module 01 - Diving Into React
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Pet Parade
Not Read
Challenge 1
Not Attempted
Quiz 1
Not Attempted
Module 02 - Components and Props
Not Attempted
NameProgress
Project: Profile Card Creator
Not Read
Key Concepts
Not Read
Challenge 2
Not Attempted
Quiz 2
Not Attempted
Key Concepts
Project: Magic Garden
Challenge 3
Not Attempted
Quiz 3
Not Attempted
Module 04 - Conditional Rendering
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Weather Wardrobe
Not Read
Challenge 4
Not Attempted
Quiz 4
Not Attempted
Module 05: Handling Events
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Interactive Quiz App"
Not Read
Challenge 5
Not Attempted
Quiz 5
Not Attempted
Module 06 - Lists and Keys
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Photo Gallery"
Not Read
Challenge 6
Not Attempted
Quiz 6
Not Attempted
Module 07 - Forms and Validation
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Registration Form"
Not Read
Challenge 7
Not Read
Quiz 7
Not Attempted
Module 08 - Lifting State Up & Composition vs Inheritance
Not Attempted
NameProgress
Key Concepts
Not Read
Project: "Weather Dashboard"
Not Read
Challenge 8
Not Attempted
Quiz 8
Not Attempted
0 / 470 XP

Overview

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.

Goals

  • Implement state management using <strong>useState</strong> to track changes in the garden.
  • Use <strong>useEffect</strong> to handle lifecycle events like growing flowers and changing environmental factors.
  • Create a visually engaging and interactive garden simulation.

Instructions

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.

  • Garden.js
import React, { useState, useEffect } from 'react';
import Flower from './Flower';

function Garden() {
  const [flowers, setFlowers] = useState([]);

  useEffect(() =&gt; {
    const interval = setInterval(() =&gt; {
      const newFlowers = flowers.map(flower =&gt; ({
        ...flower,
        size: flower.size * 1.1, // Flowers grow over time
      }));
      setFlowers(newFlowers);
    }, 1000); // Update every second to grow flowers

    return () =&gt; clearInterval(interval); // Clean up the interval on component unmount
  }, [flowers]);

  // Function to plant a new flower
  const plantFlower = () =&gt; {
    setFlowers([...flowers, { id: flowers.length, size: 1, color: 'pink' }]);
  };

  return (
    &lt;div&gt;
      &lt;button onClick={plantFlower}&gt;Plant a Flower&lt;/button&gt;
      &lt;div className="garden"&gt;
        {flowers.map(flower =&gt; (
          &lt;Flower key={flower.id} size={flower.size} color={flower.color} /&gt;
        ))}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

export default Garden;
  • Flower.js This component will represent an individual flower in the garden.
import React from 'react';

function Flower({ size, color }) {
  return (
    &lt;div style={{
      width: size,
      height: size,
      backgroundColor: color,
      borderRadius: '50%',
    }}&gt;&lt;/div&gt;
  );
}

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.

Expanding the Project

As you become more comfortable with state and effects, consider adding features like:

  • Different types of flowers with different growth rates and colors.
  • Environmental factors like weather conditions that can accelerate or inhibit growth.
  • A feature to remove or prune flowers.

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!

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline