【发布时间】:2019-06-15 06:51:03
【问题描述】:
当我尝试在我的 Web 应用程序中推送到新页面时,以下代码会出现控制台错误:
警告:无法对未安装的组件执行 React 状态更新。 这是一个空操作,但它表明您的应用程序中存在内存泄漏。 要修复,请取消 useEffect 中的所有订阅和异步任务 清理功能。 在 PasswordResetPage(由 Context.Consumer 创建)
您会在注释掉的代码中注意到我尝试使用useEffect,因为这是大多数stackoverflow 答案所指向的。但是,错误仍然存在。如何在这种情况下使用history.push 并摆脱错误?
import React, { useState /* , useEffect */ } from 'react'
import useForm from 'react-hook-form'
import Logo from '../assets/images/logo-icon-orange.png'
import TextInput from '../components/TextInput'
import Footer from '../components/Footer'
import AnimatedButton from '../components/AnimatedButton'
import logger from '../logger'
import { sleep } from '../utils/promise'
import PasswordResetSchema from './validation/PasswordResetSchema'
const PasswordResetPage = ({ history }) => {
const [isSubmitting, setSubmitting] = useState(false)
// const [isDone, setDone] = useState(false)
const { register, handleSubmit, errors } = useForm({
validationSchema: PasswordResetSchema,
})
const onSubmit = async data => {
setSubmitting(true)
await sleep(2000)
logger.info('form data', data)
// setDone(true)
history.push('/confirmation')
}
// useEffect(() => {
// if (isDone) {
// history.push('/confirmation')
// }
// })
return (
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<div className="text-center mb-4">
<img className="mb-4" src={Logo} alt="Striver" width={72} />
<h1 className="h3 mb-3 font-weight-normal">Password reset</h1>
<p>Enter your email address below and we will send you instructions on how you can reset your password.</p>
</div>
<div className="form-label-group">
<TextInput type="email" id="email" register={register} label="Email address" errors={errors} />
</div>
<AnimatedButton actionTitle="Next" isSubmitting={isSubmitting} />
<Footer />
</form>
)
}
export default PasswordResetPage
【问题讨论】:
标签: javascript reactjs react-hooks