Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't call setState (or forceUpdate) on an unmounted component

Tags:

I'm trying to fetch the data from the server after component has been updated but I couldn't manage to do that. As far as I understand componentWillUnmount is called when component is about to be destroyed, but I never need to destroy it so it's useless to me. What would be solution for this? When I should set the state?

async componentDidUpdate(prevProps, prevState) {   if (this.props.subject.length && prevProps.subject !== this.props.subject) {     let result = await this.getGrades({       student: this.props.id,       subject: this.props.subject     });     this.setState({       subject: this.props.subject,       grades: result     });   } }  async getGrades(params) {   let response, body;    if (params['subject'].length) {     response = await fetch(apiRequestString.gradesBySubject(params));     body = await response.json();   } else {     response = await fetch(apiRequestString.grades(params));     body = await response.json();   }    if (response.status !== 200) throw Error(body.message);    return body; } 

Full error:

Warning: Can't call setState (or forceUpdate) 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 the componentWillUnmount method. 
like image 348
Nikola Stojaković Avatar asked May 19 '18 19:05

Nikola Stojaković


People also ask

Can't call setState or forceUpdate on an unmounted component This is a no-op?

Warning: Can't call setState (or forceUpdate) 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 the componentWillUnmount method.

Can set state on unmounted component?

Seeing called setState() on an unmounted component in your browser console means the callback for an async operation is still running after a component's removed from the DOM. This points to a memory leak caused by doing redundant work which the user will never benefit from.

Can't call setState on a component that is not yet mounted functional component?

This error usually happens when you call the React setState method in a Class component's constructor. The reason is that the component state is not yet initialized when the constructor is called.

Can perform a React state update on an unmounted component?

The warning "Can't perform a React state update on an unmounted component" is caused when we try to update the state of an unmounted component. A straight forward way to get rid of the warning is to keep track of whether the component is mounted using an isMounted boolean in our useEffect hook.


1 Answers

A common pattern I use in this instance is something along the lines of

componentWillUnmount() {     this.isCancelled = true; } 

And then in the code where you're awaiting an async function to resolve, you would add a check before setting state:

async componentDidUpdate(prevProps, prevState) {     if (this.props.subject.length && prevProps.subject !== this.props.subject) {         let result = await this.getGrades({             student: this.props.id,             subject: this.props.subject         });         !this.isCancelled && this.setState({             subject: this.props.subject,             grades: result         });     } } 

That will stop any state setting on unmounted/unmounting components

like image 144
Steve Vaughan Avatar answered Oct 20 '22 01:10

Steve Vaughan