基本上,我有一個登錄頁面,當你填寫表格并提交資料時,它會通過這個
const formSubmitHandler = (e) => {
e.preventDefault();
//validation and other uninteresting stuff...
axios.post('/signin', { email: inputs.email, password: inputs.password })
.then(response => {
localStorage.setItem('token', JSON.stringify(response.data.token));
addUser({ id: response.data.id, name: response.data.username });
history.push('/');
})
.catch(err => setLastError(err.response.data));
}
它有效,但我收到警告
警告:無法對卸載的組件執行 React 狀態更新。這是一個空操作,但它表明您的應用程式中存在記憶體泄漏。要修復,請取消 useEffect 清理函式中的所有訂閱和異步任務。
所以我用谷歌搜索這個問題,然后嘗試添加 axios 取消令牌,更準確地說,我設定了
let source = axios.CancelToken.source();
然后將以下內容添加到我的 axios 請求中
..., { cancelToken: source.token }).then...
最后
componentWillUnmount = () => {
source.cancel();
}
但我仍然收到警告。我已經失去了幾個小時試圖擺脫這個,我失去了寶貴的時間,但似乎沒有任何效果。
此外,無論出于何種原因,如果我洗掉addUser函式,我將失去警告,但沒有任何意義,考慮到這只是 Redux 調度,據我所知,它們應該是同步的,無關緊要與組件的本地狀態。
可能是什么問題?
uj5u.com熱心網友回復:
你可以試試:
axios.post('/signin', { email: inputs.email, password: inputs.password })
.then(response => {
localStorage.setItem('token', JSON.stringify(response.data.token));
addUser({ id: response.data.id, name: response.data.username });
return <Redirect to='/'/>;
})
.catch(err => setLastError(err.response.data));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337236.html
上一篇:理解高階JavaScript函式
