[英]How to implement onClick componentDidMount() in React using Axios
[英]Do not understand 400 error when using ComponentDidMount() - React Axios
好的,所以當我使用 ComponentDidUpdate() 時,get 調用工作得很好,除了它無限循環的事實。 當我使用這段代碼時:
componentDidMount() {
const getRequest = async() => {
try {
const result = await axios.get(`https://api.openweathermap.org/data/2.5/onecall?lat=${this.state.lat}&lon=${this.state.long}&
exclude={part}&appid=${this.state.apikey}&units=imperial`);
console.log(result);
}
catch(err){
console.error(err);
}
}
getRequest();
}
我不斷收到 400 錯誤。 誰能幫我解決這個問題,我迷路了。 這是一個個人項目,而不是任何人問之前的家庭作業。
嘗試將您的異步 function 移到 componentDidMount 之外並從那里調用它。 而且您似乎還使用了一個名為part的變量。 您還應該將其提供給 function。 由於它給出了 400 錯誤,它一定與您的參數有關,主要來自您的 state。
getRequest = async (apikey, lat, long, part) => {
try {
const result = await axios.get(`https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${long}&
exclude={part}&appid=${apikey}&units=imperial`);
console.log(result);
}
catch(err){
console.error(err);
}
}
componentDidMount() {
const { apikey, lat, long } = this.state;
this.getRequest(apikey, lat, long, 'daily');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.