Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
Rendering API data

I am trying to make an API call and conditionally render data to the page. If it meets a certain criteria (date in this case), I want to display it, otherwise I don't want it to show up on the page. However, when I type in null I still get 4 items logging as "null" in my console (I only want the 'display to page' item to show up). I have followed this approach in previous projects and it has worked...anyone know what might be happening here? Any insights would be appreciated, thanks!

useEffect(() => {
axios({
  method: "get",
  url: `http://dataservice.accuweather.com/forecasts/v1/daily/5day/${userInput}`,
  params: {
    apikey: "<API_key>",
  },
})
  .then((response) => {
    setWeatherData(
      response.data.DailyForecasts.map((item) => {
        console.log(
          item.Date === "2022-05-31T07:00:00-04:00"
            ? "display to page"
            : null
        );
      })
    );
  })
  .catch((error) => {
    console.log(error);
  });
  }, [userInput]);

When mapping over it in the jsx, I did this:

 <section className="wrapper weatherContainer">
   {weatherData.map((data) => {
    console.log(data);
    return (
      <div className="topContainer">
       
        {/* if date matches today's date, display on top container, otherwise don't display anything */}
        {
          data.Date === "2022-05-31T07:00:00-04:00" ? (
            <div className="topContainer">
              <h1>Today</h1>
              <p>
                <img
                  src={require(`../../assets/${data.Day.Icon}.png`)}
                  alt={data.Day.IconPhrase}
                />
                {toCelcius(data.Temperature.Maximum.Value).toFixed(0)}&#176;
              </p>
              <p>{data.Day.IconPhrase}</p>
            </div>
          ) : null
        }
      </div>
    );
  })}
</section>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Empty <div>s show up because the first map function within <section> tags return a <div className="topContainer"> even if the item does not match. Returning the item only if it is matched does the trick.

<section className="wrapper weatherContainer">
    {weatherData.map((data) => {
    console.log(data);
    return (
        data.Date === "2022-05-31T07:00:00-04:00" ? (
            <div className="topContainer">
                <h1>Today</h1>
                ... rest of the elements
            </div>
        ) : null
    );
    })}
</section>
about 4 years ago · Juan Pablo Isaza Relatório

0

You are not logging properly. Look, in your ternary operator, you say if true - log "display to page", else log null. To log only elements which are

item.Date === "2022-05-31T07:00:00-04:00"

You need to do this way

if(item.Date === "2022-05-31T07:00:00-04:00") console.log(item)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda