这是完全错误
<块引用>警告:无法对已卸载的组件执行 React 状态更新。 这是一个空操作,但它表明您的应用程序中存在内存泄漏。 修复,取消 useEffect 中的所有订阅和异步任务 清理功能。 在注册页面
这是我的 siguppage.js 文件
import React, { useState } from 'react'
import { withFirebase } from '../firebase/Context'
import { useHistory } from 'react-router-dom'
import { compose } from 'recompose'
import withAuthUser from '../UserData/withAuthUser'
const SignUpPage = (props) => {
const [Email, setEmail] = useState('')
const [PasswordOne, setPasswordOne] = useState('')
const [PasswordTwo, setPasswordTwo] = useState('')
const [UserName, setUserName] = useState('')
const [error, setError] = useState('')
let history = useHistory()
const [AuthUser, setAuthUser] = props.AuthUser()
const onSubmit = () => {
props.firebase
.createUserWithEmailAndPassword(Email, PasswordOne)
.then((authUser) => {
history.push('/home')
setAuthUser(authUser)
})
.catch((error) => {
setError(error)
console.log(error)
})
}
const IsInvalid =
Email === '' ||
PasswordOne === '' ||
PasswordTwo === '' ||
UserName === '' ||
PasswordTwo !== PasswordOne
return (
<div>
<h1>Sign Up</h1>
<input
type='text'
placeholder='UserName'
value={UserName}
onChange={(UserName) => {
const { value } = UserName.target
setUserName(value)
}}
></input>
<input
type='text'
placeholder='email'
value={Email}
onChange={(Email) => {
const { value } = Email.target
setEmail(value)
}}
></input>
<input
type='password'
placeholder='PasswordOne'
value={PasswordOne}
onChange={(pass) => {
const { value } = pass.target
setPasswordOne(value)
}}
></input>
<input
type='password'
placeholder='PasswordTwo'
value={PasswordTwo}
onChange={(pass) => {
const { value } = pass.target
setPasswordTwo(value)
}}
></input>
<button disabled={IsInvalid} onClick={onSubmit} type='submit'>
Submit
</button>
{error && <p>{error.message}</p>}
</div>
)
}
export default compose(withFirebase, withAuthUser)(SignUpPage)
我为此使用了 HOC,我通过 withAuthUser 传递了 props.AuthUser
类似这样的东西
const withAuthUser = (Component) => (props) => {
return (
<AuthUserContext.Consumer>
{(AuthUser) => <Component {...props} AuthUser={AuthUser}></Component>}
</AuthUserContext.Consumer>
)
}
AuthUser
是一个函数
export const Value = () => {
const [AuthUser, setAuthUser] = useState(null)
return [AuthUser, setAuthUser]
}
我将此函数传递给主 index.js
中的 Provider 使用上下文
所以我试图通过调用 props.setAuthUser 来更新 AuthUser
的状态,但它给出了这个错误..
答案 0 :(得分:3)
当您在离开屏幕后尝试更新屏幕内的状态时,会出现此错误。为了解决这个问题,我们需要一个使用 useEffect
hook
所以你的 SignupPage.js
应该是这样的
import React, { useEffect, useState } from "react";
import { withFirebase } from "../firebase/Context";
import { useHistory } from "react-router-dom";
import { compose } from "recompose";
import withAuthUser from "../UserData/withAuthUser";
const SignUpPage = (props) => {
const [Email, setEmail] = useState("");
const [PasswordOne, setPasswordOne] = useState("");
const [PasswordTwo, setPasswordTwo] = useState("");
const [UserName, setUserName] = useState("");
const [error, setError] = useState("");
let history = useHistory();
const [AuthUser, setAuthUser] = props.AuthUser();
useEffect(() => {
return () => {};
}, []);
const onSubmit = () => {
props.firebase
.createUserWithEmailAndPassword(Email, PasswordOne)
.then((authUser) => {
history.push("/home");
setAuthUser(authUser);
})
.catch((error) => {
setError(error);
console.log(error);
});
};
const IsInvalid =
Email === "" ||
PasswordOne === "" ||
PasswordTwo === "" ||
UserName === "" ||
PasswordTwo !== PasswordOne;
return (
<div>
<h1>Sign Up</h1>
<input
type="text"
placeholder="UserName"
value={UserName}
onChange={(UserName) => {
const { value } = UserName.target;
setUserName(value);
}}
></input>
<input
type="text"
placeholder="email"
value={Email}
onChange={(Email) => {
const { value } = Email.target;
setEmail(value);
}}
></input>
<input
type="password"
placeholder="PasswordOne"
value={PasswordOne}
onChange={(pass) => {
const { value } = pass.target;
setPasswordOne(value);
}}
></input>
<input
type="password"
placeholder="PasswordTwo"
value={PasswordTwo}
onChange={(pass) => {
const { value } = pass.target;
setPasswordTwo(value);
}}
></input>
<button disabled={IsInvalid} onClick={onSubmit} type="submit">
Submit
</button>
{error && <p>{error.message}</p>}
</div>
);
};
export default compose(withFirebase, withAuthUser)(SignUpPage);
试试这个让我知道。
答案 1 :(得分:2)
您正在尝试在未安装的组件上设置状态。
const onSubmit = () => {
props.firebase
.createUserWithEmailAndPassword(Email, PasswordOne)
.then((authUser) => {
history.push("/home"); //This causes the current component to unmount
setAuthUser(authUser); //This is an async action, you are trying to set state on an unmounted component.
})
.catch((error) => {
setError(error);
console.log(error);
});
};