import React from "react";
import { useParams } from "react-router-dom";
import { useState, useEffect } from "react";
const SingleUser = () => {
const [user, setUser] = useState([]);
const { username } = useParams();
useEffect(() => {
const getSingleUser = async () => {
try {
const res = await fetch(
`https://api.chess.com/pub/player/${username}/stats`
);
const data = await res.json();
const heroesArray = Object.values(data);
setUser(heroesArray);
console.log(heroesArray[0].last);
} catch (error) {
console.error(error);
}
};
getSingleUser();
}, []);
return (
Rapid Chess
Current Rating: {user[0].last.rating}
Best Rating: {user[0].best.rating}
Wins: {user[0].record.win}
Losses: {user[0].record.loss}
Draws: {user[0].record.draw}
);
};
export default SingleUser;
由于某种原因,我的代码只显示一次,当我刷新页面时,我开始出现错误。我认为这与 useEffect 有关,但我不知道
类型错误:无法读取未定义的属性(读取“最后”)。我收到这些错误。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号