作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是完全错误
Warning: Can't perform a React state update on an unmounted component.This is a no-op, but it indicates a memory leak in your application.To fix, cancel all subscriptions and asynchronous tasks in a useEffectcleanup function.at SignUpPage
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,我通过了
props.AuthUser
通过 withAuthUser 这样的东西
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 使用上下文
AuthUser
的状态通过调用 props.setAuthUser 但它给出了这个错误..
最佳答案
当您在离开屏幕后尝试更新屏幕内的状态时,会出现此错误。为了解决这个问题,我们需要一个使用 useEffect
的清理函数。钩
所以你的 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);
试试这个,让我知道。
关于reactjs - 错误 : Can't perform a React state update on an unmounted component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67055556/
我是一名优秀的程序员,十分优秀!