skip to Main Content

I’ve been trying to display different messages when data from the OpenWeatherAPI (Current Weather version) gets passed in. I want a message for the different temperature and a message displayed for the different descriptions displayed at the same time. The problem is that the message isn’t updating when the data gets passed. Is there a way to fix this?

Here’s the code for the Aside.js file:

//Aside Component - used for suggesting things to wear based on the location

//DEPENCENCIES
import React, { useState } from 'react'


function Aside() {
    /* PSUEDOCODE:

    TRYING TO USE THE DATA BEING PULLED IN AND BASED ON THE DATA, THERE'S A DIFFERENT MESSAGE BASED ON THE WEATHER AND THE DESCRIPTION

        dependencies - import WeatherCard and WeatherApp so I can use the values in the WeatherCard & WeatherApp for my algorithm
    
        useState - to set the initial value of the aside. say something like, "type zipcode to get suggestions for attire"

        setMessage - to grab the information on what to say when the zip code is displayed

        ex) const setMessage = async () => {
            use if/else statements to choose which message to display. use emojis!
                an if/else statement for weather[0].description
                an if/else statement for main.temp
                an if/else statement for main.humidity
            nest all of the if/else statements in a trycatch, so if it doesn't work, it sends an error message


        useEffect - use useEffect to run the setMessage function
        ex)
        useEffect(() => {

        
        , []}) <--- This is where I put the zipcode to indicate that it wont run unless the zipcode has a value

        const display - to display the message

        return - returning the const display
    
    */


    let [message, setMessage] = useState("Type in ZipCode to get suggestions for attire!")

    /* COMMENTED OUT TO KEEP THE SITE FROM CRASHING. FINISH CODING THIS LATER
    const { zipCode } = useParams()*/

    setMessage = ({ WeatherData }) => {
        try {
            //MESSAGES FOR TEMPERATURE
            if (WeatherData.main.temp > 100) {
                message = "It's hot.... like really hot... leathers not recommended at all..."
            } else if(WeatherData.main.temp < 70) {
                message = "it's warm today! No need for bundling up"
            } else if(WeatherData.main.temp < 45) {
                message = "It's not that cold outside today, but a small sweater will do"
            } else if(WeatherData.main.temp < 20) {
                message = "Brrr, chilly! try putting on a Jacket!"
            } else {
                message = ""
            }

            //MESSAGES FOR DESCRIPTION - tell the users what to bring/buy
            if (WeatherData.weather[0].description == "clear sky") {
                message = "You might want to get some sunglasses"
            } else if (WeatherData.weather[0].description == "shower rain") {
                message = "Make sure you have an umbrella"
            } else if (WeatherData.weather[0].description == "rain" && WeatherData.weather[0].description == "thunderstorm") {
                message = "Grab an umbrella! It's pouring!"
            } else if (WeatherData.weather[0].description == "snow") {
                message = "get your snowboots 'cause its snowin outside."
            } else {
                message =""
            }

        } catch (error) {
            message = "No zipcode, no attire suggestions. sorry..."
        }
    }

    
    /* COMMENTED OUT TO KEEP THE SITE FROM CRASHING. FINISH CODING THIS LATER
    useEffect(() => {
        setMessage()
    }, [zipCode])*/

    return (
        <div className='asideContainer'>
            {message}
        </div>
    )
}




export default Aside

And here’s the import of it in the App.js file

      <Aside zipCode={zipCode} weatherData={weatherData} />

I got the initial message in the useState to pull up but I got stuck in trying to update the message. I’m very much of a beginner on using React, so I need some help

2

Answers


  1. You can use one more state for description and set states based on message/description as below-

    import React, { useState } from 'react'
    
    
    function Aside() {
        let [message, setMessage] = useState("Type in ZipCode to get suggestions for attire!")
        let [description, setDescription] = useState("");
    
        const getWeaterDetails = ({ WeatherData }) => {
          let  weatherMessage = "";
          let weatherDescription = "";
          try {
                if (WeatherData.main.temp > 100) {
                    weatherMessage = "It's hot.... like really hot... leathers not recommended at all..."
                } else if(WeatherData.main.temp < 70) {
                    weatherMessage = "it's warm today! No need for bundling up"
                } else if(WeatherData.main.temp < 45) {
                    weatherMessage = "It's not that cold outside today, but a small sweater will do"
                } else if(WeatherData.main.temp < 20) {
                    weatherMessage = "Brrr, chilly! try putting on a Jacket!"
                } else {
                    weatherMessage = ""
                }
    
                if (WeatherData.weather[0].description == "clear sky") {
                    weatherDescription = "You might want to get some sunglasses"
                } else if (WeatherData.weather[0].description == "shower rain") {
                    weatherDescription = "Make sure you have an umbrella"
                } else if (WeatherData.weather[0].description == "rain" && WeatherData.weather[0].description == "thunderstorm") {
                    weatherDescription = "Grab an umbrella! It's pouring!"
                } else if (WeatherData.weather[0].description == "snow") {
                    weatherDescription = "get your snowboots 'cause its snowin outside."
                } else {
                    weatherDescription =""
                }
                setMessage(weatherMessage);
                setDescription(weatherDescription);
    
            } catch (error) {
                weatherMessage = "No zipcode, no attire suggestions. sorry..."
            }
        };
    
        setMessage = ({ WeatherData }) => {
            
        }
    
    
    
        return (
            <div className='asideContainer'>
                {message}
                {description}
            </div>
        )
    }
    
    
    
    
    export default Aside
    
    Login or Signup to reply.
  2. There’s no need at all for any local message state as it is completely derived from the passed weatherData prop. Just compute it directly and render.

    Example:

    <Aside weatherData={weatherData} />
    
    import React from 'react'l
    
    function Aside({ weatherData }) {
      let message = "Type in ZipCode to get suggestions for attire!";
    
      // MESSAGES FOR TEMPERATURE
      if (WeatherData.main.temp > 100) {
        message = "It's hot.... like really hot... leather's not recommended at all..."
      } else if (WeatherData.main.temp < 70) {
        message = "It's warm today! No need for bundling up"
      } else if (WeatherData.main.temp < 45) {
        message = "It's not that cold outside today, but a small sweater will do"
      } else if (WeatherData.main.temp < 20) {
        message = "Brrr, chilly! try putting on a Jacket!"
      }
    
      // MESSAGES FOR DESCRIPTION - tell the users what to bring/buy
      if (WeatherData.weather[0].description == "clear sky") {
        message = "You might want to get some sunglasses"
      } else if (WeatherData.weather[0].description == "shower rain") {
        message = "Make sure you have an umbrella"
      } else if (WeatherData.weather[0].description == "rain" && 
          WeatherData.weather[0].description == "thunderstorm"
      ) {
        message = "Grab an umbrella! It's pouring!"
      } else if (WeatherData.weather[0].description == "snow") {
        message = "get your snowboots 'cause it's snowin outside."
      }
    
      return (
        <div className='asideContainer'>
          {message}
        </div>
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search