Challenge 3
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 this challenge, you will enhance the "Magic Garden" project by integrating weather effects that can influence how the garden grows. Specifically, you'll implement a feature where rain can make the flowers grow faster, demonstrating dynamic state updates based on external conditions.

Objective

  • Learn how to manipulate state dynamically based on conditions.
  • Practice using <strong>useState</strong> and <strong>useEffect</strong> to create more complex interactions within your React application.

Instructions for the Interactive Code Block

Step 1: Add a Weather State 

First, you'll need to add a new piece of state to handle the current weather condition.

// Inside Garden component
const [weather, setWeather] = ... // Weather can be 'sunny', 'rainy',...

Step 2: Implement Weather Effect on Growth 

Modify the existing <strong>useEffect</strong> that handles the growth of flowers to include the weather condition. Flowers should grow faster when it's raining.

useEffect(() =&gt; {
  const interval = setInterval(() =&gt; {
  
    const growthRate = ...
    
    const newFlowers = flowers.map(flower =&gt; ({
      ...flower,
      size: flower.size * growthRate,
    }));
    setFlowers(newFlowers);
  }, 1000); // Update every second

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

Step 3: Add Controls to Change the Weather 

Provide users with a button to toggle the weather between "sunny" and "rainy".

// Inside the return statement of Garden
&lt;button onClick={() =&gt; setWeather(weather === 'sunny' ? 'rainy' : 'sunny')}&gt;
  {weather === 'sunny' ? 'Make it rain' : 'Make it sunny'}
&lt;/button&gt;

Step 4: Styling and Testing 

Ensure that your garden and weather controls are styled appropriately. You may want to highlight the weather condition visually on the screen.

/* Add to your CSS file or within a style tag */
.weather-button {
  background-color: #blue;
  color: white;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin: 10px;
}

.garden {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

Step 5: Explore and Experiment 

Run your application and test changing the weather conditions. Observe how the growth rate of the flowers changes based on the weather. Experiment with different conditions and growth rates to see how dynamic you can make your garden.

Next Steps

After completing this challenge, consider adding more complex weather conditions, such as drought or overcast, each affecting the garden in different ways. This will not only make your garden simulation more interesting but also give you deeper insights into managing complex state dynamics in React.

This challenge is designed to enhance your understanding of state manipulation and effects in React, providing a fun and interactive way to see these concepts in action. Enjoy coding and watching your garden respond to the whims of weather!

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.
Loading...
Ready ...
Course Outline