How to perform multiple fetch calls as one asynchronous operation

What I’m trying to do is pull a name of a football player from an NFL API then use that name as a link to their page. For this error it’s combining all the links into one link which is very weird because when I was just testing all my routes without the api it worked and did not do this. It’s outputting something like this TaaotaTarlaoaortaJoshAllen to the page instead of just each player’s name then a space and another player’s name. It should be like (Bold means link):
Tua Tagovailoa Patrick Mahomes Joe Burrow Josh Allen

Side note: I feel like it’s definitely pulling data from the api just not how I want to. It’s sorta making out the names that are pulled from the api just all as one link but I want multiple links.

This is how it worked without the API and outputted like shown below:
Special Team 1 Special Team 2 Special Team 3 Special Team 4

import { Link } from "react-router-dom"
import _navbar from "../NavBar/navbar";

export default function _specialTeamsPage() {
    const specialteams = [1, 2, 3, 4, 5];

    return (
        <>
        <_navbar />
        <div>
            {specialteams.map((specialteam) => (
                <Link key={specialteam} to={`/specialteam/${specialteam}`}>
                    Special Team {specialteam}
                </Link>
            ))}
        </div>
        </>
    );

}

This is the code block with the API that is not working:

import { Link } from "react-router-dom";
import _navbar from "../NavBar/navbar";
import React, { useEffect, useState } from "react";
import styles from './card.module.css';

export default function _quarterbacksPage() {
    const quarterbacks = [3139477, 4241479, 3918298, 3915511, 2577417];
    const [names, setNames] = useState([]);
    for (let i = 0; i < quarterbacks.length; i++){
        const options = {
            method: 'GET',
            headers: {
                'x-rapidapi-key': 'secret key',
                'x-rapidapi-host': 'nfl-api-data.p.rapidapi.com'
            }
        };
    
        const FullNameUrl = 'https://nfl-api-data.p.rapidapi.com/nfl-ath-fullinfo?id=' + quarterbacks[i];
        const fetchFullName = async () => {
            fetch(FullNameUrl, options)
            .then(response => response.json())
            .then(response => {
                const name = response.athlete.fullName;
                setNames([...names, ...name]);
                
            })
        };
    
        useEffect(() => {
            fetchFullName();
        }, []);
    }


    return (
        <>
            <div>
                <_navbar />
                {names.map((name) => (
                    <Link key={name} to={`/quarterback/${name}`}>
                    {name}
                    </Link>

                ))}
            
            </div>
        </>
    );
}

There are several problems here

  1. Don’t call useEffect() in a loop
  2. When setting state based on the current value, use the functional version of the setter
  3. Using spread syntax on a string breaks it down to an array of characters
  4. What happens if players have the same name? You shouldn’t use it as the key prop when the id is much more suitable

What you should do instead is perform all your fetch calls as one asynchronous operation and then set the entire state.

For example

// these functions can be defined outside your component
const fetchFullName = async (id) => {
  const res = await fetch(
    `https://nfl-api-data.p.rapidapi.com/nfl-ath-fullinfo?id=${encodeURIComponent(id)}`,
    {
      headers: {
        "x-rapidapi-key": "secret key",
        "x-rapidapi-host": "nfl-api-data.p.rapidapi.com",
      },
    },
  );
  if (!res.ok) {
    throw new Error(`Name lookup for id '${id}' failed`);
  }
  return (await res.json()).athlete.fullName;
};

// returns an array of {id, name} objects
const fetchFullNames = async (ids) =>
  Promise.all(ids.map(async (id) => ({ id, name: await fetchFullName(id) })));
// is this meant to be a static value?
const quarterbacks = [3139477, 4241479, 3918298, 3915511, 2577417];

export default function _quarterbacksPage() {
  const [names, setNames] = useState([]);

  useEffect(() => {
    fetchFullNames(quarterbacks).then(setNames).catch(console.error);
  }, []);

  return (
    <div>
      <_navbar />
      {names.map(({ id, name }) => (
        <Link key={id} to={`/quarterback/${id}`}>
          {name}
        </Link>
      ))}
    </div>
  );
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật