我有個問題。我在其中創建了一個 Link 元素和一個 CustomButton 元素,并且在該按鈕上我有 onClick 處理程式。
< Link to = "/dashboard"
style = {
linkStyles
} >
<
CustomButton text = "Login"
onClick = {
handleSubmit
}
/> <
/Link>
以上是按鈕和鏈接的代碼
const handleSubmit = (e: any) => {
if (email !== user.email || password !== user.password) {
setError("Please make sure you have the correct email or password");
} else {
setError("");
}
};
這是處理程式。當我單擊時,我如何才能做到這一點,我不會立即被重定向,而是讓處理程式在密碼或電子郵件不正確時顯示錯誤
uj5u.com熱心網友回復:
可以添加阻止默認值并useHistory導航
const history = useHistory();
const handleSubmit = (e: any) => {
e.preventDefault();
let timeout = 0
if (email !== user.email || password !== user.password) {
setError("Please make sure you have the correct email or password");
timeout = 3000 // show the error for 3 seconds
} else {
setError("");
}
setTimeout(() => {history.push('/dashboard')}, timeout)
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/533329.html
上一篇:避免創建重復的類實體
