Project: Weather Wardrobe
by Andrew Nguyenยท Module 04 - Conditional Rendering

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
Module 03 - State and Effect
Not Attempted
NameProgress
Key Concepts
Not Read
Project: Magic Garden
Not Read
Challenge 3
Not Attempted
Quiz 3
Not Attempted
Key Concepts
Project: Weather Wardrobe
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

Objective: Create an application that recommends clothing based on the weather conditions.

Description: In this project, you will build a simple application that shows different clothing recommendations based on the current weather conditions. The app will render different outfits for sunny, rainy, snowy, and windy weather.

Instructions:

  1. Setup Your Project:
    • Create a new React project using Vite.
    • Set up your project structure with a main <strong>App</strong> component and a <strong>WeatherWardrobe</strong> component.
  2. Define Weather Conditions:
    • In the <strong>WeatherWardrobe</strong> component, create a piece of state to store the current weather condition.
    • Use a variable or hard-coded value for now (e.g., 'sunny', 'rainy', 'snowy', 'windy').
  3. Render Different Outfits:
    • Based on the weather condition, render different outfits.
    • Use conditional rendering to show different images or descriptions for each weather type.
  4. Styling:
    • Add some basic CSS to style your wardrobe items and make the app visually appealing.

Example Code:

import React, { useState } from 'react';
import './WeatherWardrobe.css';

function WeatherWardrobe() {
  const [weather, setWeather] = useState('sunny');

  let outfit;
  if (weather === 'sunny') {
    outfit = &lt;div className="outfit"&gt;๐ŸŒž Wear sunglasses and a t-shirt!&lt;/div&gt;;
  } else if (weather === 'rainy') {
    outfit = &lt;div className="outfit"&gt;๐ŸŒง๏ธ Don't forget your umbrella and raincoat!&lt;/div&gt;;
  } else if (weather === 'snowy') {
    outfit = &lt;div className="outfit"&gt;โ„๏ธ Bundle up with a warm coat and gloves!&lt;/div&gt;;
  } else if (weather === 'windy') {
    outfit = &lt;div className="outfit"&gt;๐ŸŒฌ๏ธ A windbreaker and scarf will keep you cozy!&lt;/div&gt;;
  }

  return (
    &lt;div className="weather-wardrobe"&gt;
      &lt;h1&gt;Weather Wardrobe&lt;/h1&gt;
      &lt;div className="outfit-container"&gt;
        {outfit}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

export default WeatherWardrobe;

Styling (WeatherWardrobe.css):

.weather-wardrobe {
  text-align: center;
  font-family: Arial, sans-serif;
}

.outfit-container {
  margin-top: 20px;
}

.outfit {
  font-size: 24px;
  margin-top: 20px;
}

Detailed Walkthrough:

  1. Setting Up the Project:
    • Use the command <strong>npm create vite@latest weather-wardrobe --template react</strong> to create a new Vite React project.
    • Navigate to your project directory and install the necessary dependencies using <strong>npm install</strong>.
  2. Creating the <strong>WeatherWardrobe</strong> Component:
    • Create a new file named <strong>WeatherWardrobe.jsx</strong>.
    • Import <strong>useState</strong> from React and define the <strong>WeatherWardrobe</strong> function component.
  3. Implementing Conditional Rendering:
    • Initialize the state for the current weather condition using <strong>useState</strong>.
    • Use conditional rendering (if-else statements or ternary operators) to render different outfits based on the weather condition.
  4. Styling Your Application:
    • Create a CSS file named <strong>WeatherWardrobe.css</strong>.
    • Add basic styles to center your content and style the outfit recommendations.
  5. Testing Your Application:
    • Test the application by manually changing the weather condition state and observing the rendered outfits.

By completing this project, you'll get hands-on experience with conditional rendering in React, making your learning experience both fun and practical.

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