Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : Can't perform a React state update on an unmounted component

Tags:

reactjs

this is full error

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 useEffect cleanup function. at SignUpPage

and this is my siguppage.js file

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)

I have used HOC for this, I passed the props.AuthUser via withAuthUser something like this

const withAuthUser = (Component) => (props) => {
    return (
        <AuthUserContext.Consumer>
            {(AuthUser) => <Component {...props} AuthUser={AuthUser}></Component>}
        </AuthUserContext.Consumer>
    )
}

AuthUser is a function

export const Value = () => {
    const [AuthUser, setAuthUser] = useState(null)
    return [AuthUser, setAuthUser]
}

and I passed this function to Provider usign context in main index.js

So I am trying to update state of AuthUser by calling props.setAuthUser but it gives this error..

like image 225
sanket kheni Avatar asked Apr 12 '21 09:04

sanket kheni


People also ask

How would you solve can't perform a React state update on an unmounted component?

To solve the "Warning: Can't perform a React state update on an unmounted component", declare an isMounted boolean in your useEffect hook that is used to track whether the component is mounted. A component's state should only be updated if the component is mounted.

How do you fix 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?

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 useEffect cleanup function.

What is unmounted component in React?

In the unmounting (or deletion, or "cleanup") phase, we have just one lifecycle method to help us out: componentWillUnmount . componentWillUnmount is the last function to be called immediately before the component is removed from the DOM.

Why can't I perform a state update on an unmounted component?

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 useEffect cleanup function. at SignUpPage

Why is my react app not updating state?

This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. at SetStateWarning (http://localhost:3000/static/js/bundle.js:639:80) This warning is pointing out that we can’t perform a React state update on an unmounted component.

What happens if we do not clear timeout during unmounting of component?

If we do not clear timeout or interval during unmounting of component then function will run and there will be no component available to update. Another reason for getting this warning is to use asynchronous api calls without unsubscribing them during unmount. The solution of our problem is to prevent updating state after a component is unmounted.

Why am I getting this common State Update warning?

We saw how a simple component with an asynchronous state update may yield this common warning, think about all those components you have with a similar case. Make sure you check if the component is actually mounted before you perform a state update.


Video Answer


1 Answers

You are trying to set state on an unmounted component.

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);
    });
};
like image 164
Adil Khalil Avatar answered Sep 26 '22 14:09

Adil Khalil