Challenge: Weather card
- React course
- Beginner
- Lesson 81/84
- 6 min
Write a component that takes a city name prop and shows temperature and condition.
Challenge: Weather card
This challenge asks you to write a WeatherCard component. It should take three props: city, temp, and condition. If it's very cold (temp < 0), the card background is blue; otherwise orange.
Set the className with a ternary: temp < 0 ? "cold" : "warm". Show <h2>{ '{ city }' }</h2>, <p>{ '{ temp }' }°C</p>, and <p>{ '{ condition }' }</p>.
Time to combine props destructuring and conditional rendering. Few hints; apply what you learned.
Practice Write WeatherCard
Complete the skeleton. Write a WeatherCard component. Take ({ '{ city, temp, condition }' }) as parameter. Create the root div with <div className={ '{ temp < 0 ? "cold" : "warm" }' }>. Inside, have <h2>{ '{ city }' }</h2>, <p>{ '{ temp }' }°C</p>, and <p>{ '{ condition }' }</p>. Export it.
Starter code
// Write the WeatherCard component hereSample solution
function WeatherCard({ city, temp, condition }) {
return (
<div className={temp < 0 ? "cold" : "warm"}>
<h2>{city}</h2>
<p>{temp}°C</p>
<p>{condition}</p>
</div>
);
}
export default WeatherCard;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If temp is below 0, which class?
- hot
- warm
- cold Correct answer
- cool
Explanation: temp < 0 picks the "cold" class.
2 Match the weather parts
| Term | Meaning |
|---|---|
city |
City |
temp |
Temperature |
condition |
Condition |
cold |
Low temp |