0

I am trying to get icon names from a weather API, and have my react display the icons.

The first part is working, the icon names do appear in the console log. The second part does not...

import { useSelector } from "react-redux";

export default function Icon() {
  const icons = useSelector(
    (state) =>
      state.api.map((i) => {
        console.log(i);
        i.city.list.map((ii) => {
          console.log(ii);
          ii.weather.map((iii) => {
            console.log("icon", iii.icon);
            return iii.icon;
          });
        });
      })
    //state.api[0].city.list[0].weather[0].icon
  );
  console.log(icons);
  return (
    <div>
      <img src={`https://openweathermap.org/img/wn/${icons}.png`} />
    </div>
  );
}

Any ideas?

7
  • you need to render a map, Icons does not render anything and should only throw an error. Commented Jul 18, 2022 at 15:12
  • What does icons log into the console? Commented Jul 18, 2022 at 15:14
  • the console log on line 10 gives a name of an icon when the component gets called. render method? it's react with hooks ;) Commented Jul 18, 2022 at 15:20
  • Generally speaking you should also be able to destructure things that you want from state. What is your state model? It should help us in telling you how to do that mapping properly. Commented Jul 18, 2022 at 15:31
  • there is a commented bit for that... Commented Jul 18, 2022 at 15:51

2 Answers 2

1

From what you've provided so far it seems to work like so: Make an API call to api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API key} and it gives you the response. From that response icon data can be reached for example like this:

  const obj = api response as javascript object...
  const icon = obj.list[0].weather[0].icon;

and used like you've shown:

<img src={`https://openweathermap.org/img/wn/${icon}.png`} alt="icon" />

However, we can't see what your redux state model is and how requests are eventually saved to state and so on. Which is why it is not obvious what

//state.api[0].city.list[0].weather[0].icon

does as we don't know anything about state.api[0]. This is why it is essential to provide enough information to help you solve the problem at hand. You could've just posted us the openweather data api response example or even better, redux state from that useSelector without any mappings.

People are hesitant to help when they need to get credentials for an API when you could've just provided the information to begin with.

Not trying to be rude here, but make it as easy as possible to help you and things get resolved very quickly :) this is certainly not a tough one to solve when you have all the info needed.

You could show us the state completely and we'll solve it from there or probably come to the conclusion that there is some problem with the first part, meaning state.api[0] or state.api[0].city We can't know how you save the state, but in API you provided city for example doesn't have icon, but that list owns icons. But the tricky part here is that this is all quessing without the actual state.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your comments @axordahaxor! the truth is I am a bit reluctant to ask for a solution, as I am trying to learn! Will try to follow your guidance, and, if unsuccessful, will add the requested info. Again, many thanks!
1

Please provide more info

Without knowing an example of the icons variable, there is not much help that we can provide.

For now however, I will infer from the code that icons holds an array or some iterable content.

In my experience with java, it will try to apply the array object to the image src which will of course not work.

Generic example of what may work

What you should do is map the icons similar to this:

import { useSelector } from "react-redux";

export default function Icon() {
  const icons = useSelector(
    (state) =>
      state.api.map((i) => {
        console.log(i);
        i.city.list.map((ii) => {
          console.log(ii);
          ii.weather.map((iii) => {
            console.log("icon", iii.icon);
            return iii.icon;
          });
        });
      })
    //state.api[0].city.list[0].weather[0].icon
  );
  console.log(icons);
  return (
    <div>
      {icons.map((e,i)=>{
          return <img src={`https://openweathermap.org/img/wn/${e}.png`} key={"iconsImg"+i}/>
      })}
    </div>
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.