错误:无法对卸载的组件执行 React 状态更新

san*_*eni 5 reactjs

这是完全错误

警告:无法对卸载的组件执行 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)
Run Code Online (Sandbox Code Playgroud)

我为此使用了 HOC,我通过了props.AuthUserwithAuthUser 类似的东西

const withAuthUser = (Component) => (props) => {
    return (
        <AuthUserContext.Consumer>
            {(AuthUser) => <Component {...props} AuthUser={AuthUser}></Component>}
        </AuthUserContext.Consumer>
    )
}
Run Code Online (Sandbox Code Playgroud)

AuthUser 是一个函数

export const Value = () => {
    const [AuthUser, setAuthUser] = useState(null)
    return [AuthUser, setAuthUser]
}
Run Code Online (Sandbox Code Playgroud)

我将此函数传递给了 main 中的 Provider 使用上下文 index.js

所以我试图AuthUser通过调用 props.setAuthUser来更新状态,但它给出了这个错误..

Adi*_*lil 3

您正在尝试在未安装的组件上设置状态。

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);
    });
};
Run Code Online (Sandbox Code Playgroud)