1 回答
TA贡献1719条经验 获得超6个赞
一旦承诺履行,您可以使用标志通知您。
如果不是,则向 DOM 返回一个空值。
const Temp = () => {
const [temp, setTemp] = useState("Not Changed");
const [isLoaded, setIsLoaded] = useState(false);
const SERVER_URL = "http://localhost:8000";
const token = Cookies.get("token");
const config = {
headers: {
Authorization: `Jwt ${token}`,
},
};
Axios.get(`${SERVER_URL}/accounts/4`, config)
.then((res) => {
setTemp(res.data.data.first_name);
// set flag to true once the val you want is received
setIsLoaded(true);
})
.catch((err) => {
console.log(err);
});
// display only once temp is set with val from server
if (isLoaded) {
return <h1>HI I'm {temp}!!</h1>;
}
// return null to display empty
return null;
};
添加回答
举报