1 回答

TA貢獻(xiàn)1719條經(jīng)驗 獲得超6個贊
一旦承諾履行,您可以使用標(biāo)志通知您。
如果不是,則向 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;
};
添加回答
舉報