我需要获取将用于我正在呈现的页面的数据。我目前正在 useEffect 挂钩中获取数据。我不认为在渲染中使用数据之前所有数据都已加载。当我尝试在芯片标签中使用它时,它给了我一个错误“未定义的财产姓氏”。
我不确定我应该在哪里或如何处理数据收集,因为它将在整个页面呈现时使用。我应该收集App功能之外的数据吗?
const App = (props) => {
const [teams] = useState(["3800", "0200", "0325", "0610", "0750", "0810"]);
const [players, setPlayers] = useState([]);
useEffect(() => {
teams.forEach(teamId => {
axios.defaults.headers.common['Authorization'] = authKey;
axios.get(endPoints.roster + teamId)
.then((response) => {
let teamPlayers = response.data.teamPlayers;
teamPlayers.forEach(newPlayer => {
setPlayers(players => [...players, newPlayer]);
})
})
.catch((error) => {
console.log(error);
})
});
}, []);
let numPlayersNode =
<Chip
variant="outlined"
size="small"
label={players[1].lastName}
/>
return (...