Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning: Can't call setState on a component that is not yet mounted #174

Closed
kart1ka opened this issue Apr 13, 2022 · 5 comments
Closed

Warning: Can't call setState on a component that is not yet mounted #174

kart1ka opened this issue Apr 13, 2022 · 5 comments

Comments

@kart1ka
Copy link

kart1ka commented Apr 13, 2022

I am getting 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. warning in my console (See attached image below) -

image

I have reproduced the same warning message in my GitHub repo linked below:

https://github.com/kart1ka/form-builder

Just download the code and run npm install and then npm start and you will see the warning message in the console.

The message says that the error is coming from the Preview file of the React Form Builder-2 library.
Can you please take a look at it?
Thank You.

PS - The warning disappears when the <React.StrictMode> is removed from index.js file.

@Kiho
Copy link
Owner

Kiho commented Apr 14, 2022

Please try find answer there - #92

@kart1ka
Copy link
Author

kart1ka commented Apr 14, 2022

I already looked at that issue. I don't think it has the solution to my problem. The link mentioned in that issue suggests a solution for error occurring because of async tasks. But in the repo that I have linked above I do not have any async task running. All I have is a simple Form Builder, a Form Render & a Demobar component in my code. So the warning could not have come from an async task.
Can you please take a look at my repo?
https://github.com/kart1ka/form-builder

@Kiho
Copy link
Owner

Kiho commented Apr 15, 2022

RisingStack/react-easy-state#196 (comment)
Sorry, but I don't have clear idea on this issue.

@kart1ka
Copy link
Author

kart1ka commented Apr 15, 2022

I think I might have figured out why this is happening. This might have to do with the handling of the code related to the subscription in the library.
I googled a bit & found that the subscriptions must be handled inside the componentDidMount and not inside the constructor function of the component.

Below is a link for reference:

https://www.digitalocean.com/community/tutorials/react-constructors-with-react-components#:~:text=For%20things%20like%20subscriptions%20that%20will%20update%20the%20state%20and%20other%20side%2Deffects%2C%20you%20should%20put%20the%20logic%20out%20in%20componentDidMount()%20which%20will%20fire%20immediately%20after%20the%20component%20is%20mounted%20and%20won%E2%80%99t%20give%20you%20any%20trouble%20if%20you%20update%20the%20state%20inside%20of%20it.

So I tried to implement this. As you can see in the image below, I was getting error in three files of React FormBuilder library - Demobar, Preview, and Toolbar.

image

As you can see in the image below, in the Demobar.js file, the code related to the subscription is also present inside the constructor function.

code3

So, in the Demobar.js file, I moved that code to the componentDidMount function and, as you can see in the image below, the error related to the Demobar file disappeared. It is only showing errors in the Preview & Toolbar files.

image

code2

Can you please move the lines related to the subsciption, in the Preview.jsx & Toolbar.jsx files, to the componentDidMount from the constructor function?
This might solve the issue.
I have marked the lines, that I think should be moved to the componentDidMount function in the images below:

code4

code7

Thank You.

@Kiho Kiho mentioned this issue Apr 16, 2022
@Kiho
Copy link
Owner

Kiho commented Apr 16, 2022

Looks like that was the reason of warning, Thanks.

@Kiho Kiho closed this as completed Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants