0

enter image description here

I loop through this array like this:

{props.choosenMovie.characters.map((characters) => (
   <p>{characters}</p> /* This displays the URL of course */
))}

These URL's include a name object which is what i want to display, what is the best practice to do this?


This is how it is displayed on my application, but the desire is to display the name object from the URL's.

enter image description here

5
  • What exactly is the problem? Commented Jun 15, 2020 at 13:19
  • On a different note, if possible I recommend to enhance your API to accept multiple "people" at once and not looping various calls unless really required. If you have can change the API that is. Edit: Never mind.. seems to be some public API. Commented Jun 15, 2020 at 13:21
  • Sorry, the list is displaying the URL's from the object. But i want the list to retrieve the name object from the URL. Commented Jun 15, 2020 at 13:21
  • @Arasto where is the name object in the url you shown? Commented Jun 15, 2020 at 13:30
  • @PrakashReddyPotlapadu All the url's include a name object. if we use swapi.dev/api/people/56 as an example. I want it to return "Saesee Tiin"(since it's the name of the object). My desire is that my array of URL's should be looped through and do http calls and return the name of these characters. Commented Jun 15, 2020 at 13:33

2 Answers 2

1

In useEffect, map thru your array of urls and make the api call and store the promises in an array. Use promise.all and update the state which will cause re-render.

In render method map thru the updated state and display the names.

see working demo

Code snippet

export default function App() {
  const [char, setChar] = useState([
    "https://swapi.dev/api/people/1/",
    "https://swapi.dev/api/people/2/"
  ]);
  const [people, setPeople] = useState([]);
  useEffect(() => {
    const promiseArray = [];
    char.forEach(c => {
      promiseArray.push(fetch(c).then(res => res.json()));
      Promise.all(promiseArray).then(res => {
        console.log("res", res);
        setPeople(res);
      });
    });
  }, []);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      {people.map((p, i) => {
        return <p key={i}>{p.name}</p>;
      })}
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is what i was looking for, thank you for sharing your knowledge <3
0

I was working with that API some time ago, and the way I approached it (to display the names etc) was with Promise.all

so the snipped looked like

axios.get(`https://swapi.dev/api/${this.props.match.path.split('/')[1]}/${this.props.match.params.id}/`).then((res) => {

            let characters = []

            // get all characters in the movie data
            let characterPromises = []
            res.data.characters.forEach((character) => {
                characterPromises.push(axios.get(character))
            })

            // Create list with all characters names and link to page
            Promise.all(characterPromises).then((res) => {
                res.forEach((character, i) => {
                    characters.push(<li key={i}><Link to={`/${character.data.url.split('api/')[1]}`}>{character.data.name}</Link></li>)
                })

                this.setState({
                    characters
                })
          })
  })
}

then I just used the characters lists (from state) in the render method

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.