gpt4 book ai didi

reactjs - 错误 : Can't perform a React state update on an unmounted component

转载 作者:行者123 更新时间:2023-12-03 16:19:16 25 4
gpt4 key购买 nike

这是完全错误

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


这是我的 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,我通过了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com