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












1














Problem



I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after componentWillUnmount(...).



I looked very carefully at my code and tried to put some guarding clauses in place, but the problem persisted and I am still observing the warning.



Therefore, I've got two questions:





  1. How do I figure out from the stack trace, which particular component and event handler or lifecycle hook is responsible for the rule violation?

  2. Well, how to fix the problem itself, because my code was written with this pitfall in mind and is already trying to prevent it, but some underlying component's still generating the warning.


Browser console



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 the componentWillUnmount
method.
in TextLayerInternal (created by Context.Consumer)
in TextLayer (created by PageInternal) index.js:1446
d/console[e]
index.js:1446
warningWithoutStack
react-dom.development.js:520
warnAboutUpdateOnUnmounted
react-dom.development.js:18238
scheduleWork
react-dom.development.js:19684
enqueueSetState
react-dom.development.js:12936
./node_modules/react/cjs/react.development.js/Component.prototype.setState
react.development.js:356
_callee$
TextLayer.js:97
tryCatch
runtime.js:63
invoke
runtime.js:282
defineIteratorMethods/</prototype[method]
runtime.js:116
asyncGeneratorStep
asyncToGenerator.js:3
_throw
asyncToGenerator.js:29


enter image description here



Code



Book.tsx



import { throttle } from 'lodash';
import * as React from 'react';
import { AutoWidthPdf } from '../shared/AutoWidthPdf';
import BookCommandPanel from '../shared/BookCommandPanel';
import BookTextPath from '../static/pdf/sde.pdf';
import './Book.css';

const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: () => void;
pdfWrapper: HTMLDivElement | null = null;
isComponentMounted: boolean = false;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

constructor(props: any) {
super(props);
this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
);
}

componentDidMount = () => {
this.isComponentMounted = true;
this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
this.isComponentMounted = false;
window.removeEventListener("resize", this.setDivSizeThrottleable);
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
bookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
bookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;


AutoWidthPdf.tsx



import * as React from 'react';
import { Document, Page, pdfjs } from 'react-pdf';

pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

interface IProps {
file: string;
width: number;
onLoadSuccess: (pdf: any) => void;
}
export class AutoWidthPdf extends React.Component<IProps> {
render = () => (
<Document
file={this.props.file}
onLoadSuccess={(_: any) => this.props.onLoadSuccess(_)}
>
<Page
pageNumber={1}
width={this.props.width}
/>
</Document>
);
}




Update 1: Cancel throttleable function (still no luck)



const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: ((() => void) & Cancelable) | undefined;
pdfWrapper: HTMLDivElement | null = null;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

componentDidMount = () => {
this.setDivSizeThrottleable = throttle(
() => {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
},
500,
);

this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
window.removeEventListener("resize", this.setDivSizeThrottleable!);
this.setDivSizeThrottleable!.cancel();
this.setDivSizeThrottleable = undefined;
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
BookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
BookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable!();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;









share|improve this question
























  • Does the problem persist if you comment out the add & remove listeners?
    – ic3b3rg
    Dec 27 '18 at 19:20










  • @ic3b3rg the problem disappears if there's no event listening code
    – Igor Soloydenko
    Dec 27 '18 at 20:26










  • ok, did you try the suggestion to do this.setDivSizeThrottleable.cancel() instead of the this.isComponentMounted guard?
    – ic3b3rg
    Dec 27 '18 at 20:30










  • @ic3b3rg Still the same run-time warning.
    – Igor Soloydenko
    Dec 27 '18 at 23:38
















1














Problem



I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after componentWillUnmount(...).



I looked very carefully at my code and tried to put some guarding clauses in place, but the problem persisted and I am still observing the warning.



Therefore, I've got two questions:





  1. How do I figure out from the stack trace, which particular component and event handler or lifecycle hook is responsible for the rule violation?

  2. Well, how to fix the problem itself, because my code was written with this pitfall in mind and is already trying to prevent it, but some underlying component's still generating the warning.


Browser console



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 the componentWillUnmount
method.
in TextLayerInternal (created by Context.Consumer)
in TextLayer (created by PageInternal) index.js:1446
d/console[e]
index.js:1446
warningWithoutStack
react-dom.development.js:520
warnAboutUpdateOnUnmounted
react-dom.development.js:18238
scheduleWork
react-dom.development.js:19684
enqueueSetState
react-dom.development.js:12936
./node_modules/react/cjs/react.development.js/Component.prototype.setState
react.development.js:356
_callee$
TextLayer.js:97
tryCatch
runtime.js:63
invoke
runtime.js:282
defineIteratorMethods/</prototype[method]
runtime.js:116
asyncGeneratorStep
asyncToGenerator.js:3
_throw
asyncToGenerator.js:29


enter image description here



Code



Book.tsx



import { throttle } from 'lodash';
import * as React from 'react';
import { AutoWidthPdf } from '../shared/AutoWidthPdf';
import BookCommandPanel from '../shared/BookCommandPanel';
import BookTextPath from '../static/pdf/sde.pdf';
import './Book.css';

const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: () => void;
pdfWrapper: HTMLDivElement | null = null;
isComponentMounted: boolean = false;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

constructor(props: any) {
super(props);
this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
);
}

componentDidMount = () => {
this.isComponentMounted = true;
this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
this.isComponentMounted = false;
window.removeEventListener("resize", this.setDivSizeThrottleable);
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
bookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
bookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;


AutoWidthPdf.tsx



import * as React from 'react';
import { Document, Page, pdfjs } from 'react-pdf';

pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

interface IProps {
file: string;
width: number;
onLoadSuccess: (pdf: any) => void;
}
export class AutoWidthPdf extends React.Component<IProps> {
render = () => (
<Document
file={this.props.file}
onLoadSuccess={(_: any) => this.props.onLoadSuccess(_)}
>
<Page
pageNumber={1}
width={this.props.width}
/>
</Document>
);
}




Update 1: Cancel throttleable function (still no luck)



const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: ((() => void) & Cancelable) | undefined;
pdfWrapper: HTMLDivElement | null = null;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

componentDidMount = () => {
this.setDivSizeThrottleable = throttle(
() => {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
},
500,
);

this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
window.removeEventListener("resize", this.setDivSizeThrottleable!);
this.setDivSizeThrottleable!.cancel();
this.setDivSizeThrottleable = undefined;
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
BookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
BookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable!();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;









share|improve this question
























  • Does the problem persist if you comment out the add & remove listeners?
    – ic3b3rg
    Dec 27 '18 at 19:20










  • @ic3b3rg the problem disappears if there's no event listening code
    – Igor Soloydenko
    Dec 27 '18 at 20:26










  • ok, did you try the suggestion to do this.setDivSizeThrottleable.cancel() instead of the this.isComponentMounted guard?
    – ic3b3rg
    Dec 27 '18 at 20:30










  • @ic3b3rg Still the same run-time warning.
    – Igor Soloydenko
    Dec 27 '18 at 23:38














1












1








1







Problem



I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after componentWillUnmount(...).



I looked very carefully at my code and tried to put some guarding clauses in place, but the problem persisted and I am still observing the warning.



Therefore, I've got two questions:





  1. How do I figure out from the stack trace, which particular component and event handler or lifecycle hook is responsible for the rule violation?

  2. Well, how to fix the problem itself, because my code was written with this pitfall in mind and is already trying to prevent it, but some underlying component's still generating the warning.


Browser console



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 the componentWillUnmount
method.
in TextLayerInternal (created by Context.Consumer)
in TextLayer (created by PageInternal) index.js:1446
d/console[e]
index.js:1446
warningWithoutStack
react-dom.development.js:520
warnAboutUpdateOnUnmounted
react-dom.development.js:18238
scheduleWork
react-dom.development.js:19684
enqueueSetState
react-dom.development.js:12936
./node_modules/react/cjs/react.development.js/Component.prototype.setState
react.development.js:356
_callee$
TextLayer.js:97
tryCatch
runtime.js:63
invoke
runtime.js:282
defineIteratorMethods/</prototype[method]
runtime.js:116
asyncGeneratorStep
asyncToGenerator.js:3
_throw
asyncToGenerator.js:29


enter image description here



Code



Book.tsx



import { throttle } from 'lodash';
import * as React from 'react';
import { AutoWidthPdf } from '../shared/AutoWidthPdf';
import BookCommandPanel from '../shared/BookCommandPanel';
import BookTextPath from '../static/pdf/sde.pdf';
import './Book.css';

const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: () => void;
pdfWrapper: HTMLDivElement | null = null;
isComponentMounted: boolean = false;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

constructor(props: any) {
super(props);
this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
);
}

componentDidMount = () => {
this.isComponentMounted = true;
this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
this.isComponentMounted = false;
window.removeEventListener("resize", this.setDivSizeThrottleable);
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
bookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
bookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;


AutoWidthPdf.tsx



import * as React from 'react';
import { Document, Page, pdfjs } from 'react-pdf';

pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

interface IProps {
file: string;
width: number;
onLoadSuccess: (pdf: any) => void;
}
export class AutoWidthPdf extends React.Component<IProps> {
render = () => (
<Document
file={this.props.file}
onLoadSuccess={(_: any) => this.props.onLoadSuccess(_)}
>
<Page
pageNumber={1}
width={this.props.width}
/>
</Document>
);
}




Update 1: Cancel throttleable function (still no luck)



const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: ((() => void) & Cancelable) | undefined;
pdfWrapper: HTMLDivElement | null = null;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

componentDidMount = () => {
this.setDivSizeThrottleable = throttle(
() => {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
},
500,
);

this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
window.removeEventListener("resize", this.setDivSizeThrottleable!);
this.setDivSizeThrottleable!.cancel();
this.setDivSizeThrottleable = undefined;
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
BookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
BookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable!();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;









share|improve this question















Problem



I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after componentWillUnmount(...).



I looked very carefully at my code and tried to put some guarding clauses in place, but the problem persisted and I am still observing the warning.



Therefore, I've got two questions:





  1. How do I figure out from the stack trace, which particular component and event handler or lifecycle hook is responsible for the rule violation?

  2. Well, how to fix the problem itself, because my code was written with this pitfall in mind and is already trying to prevent it, but some underlying component's still generating the warning.


Browser console



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 the componentWillUnmount
method.
in TextLayerInternal (created by Context.Consumer)
in TextLayer (created by PageInternal) index.js:1446
d/console[e]
index.js:1446
warningWithoutStack
react-dom.development.js:520
warnAboutUpdateOnUnmounted
react-dom.development.js:18238
scheduleWork
react-dom.development.js:19684
enqueueSetState
react-dom.development.js:12936
./node_modules/react/cjs/react.development.js/Component.prototype.setState
react.development.js:356
_callee$
TextLayer.js:97
tryCatch
runtime.js:63
invoke
runtime.js:282
defineIteratorMethods/</prototype[method]
runtime.js:116
asyncGeneratorStep
asyncToGenerator.js:3
_throw
asyncToGenerator.js:29


enter image description here



Code



Book.tsx



import { throttle } from 'lodash';
import * as React from 'react';
import { AutoWidthPdf } from '../shared/AutoWidthPdf';
import BookCommandPanel from '../shared/BookCommandPanel';
import BookTextPath from '../static/pdf/sde.pdf';
import './Book.css';

const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: () => void;
pdfWrapper: HTMLDivElement | null = null;
isComponentMounted: boolean = false;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

constructor(props: any) {
super(props);
this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
);
}

componentDidMount = () => {
this.isComponentMounted = true;
this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
this.isComponentMounted = false;
window.removeEventListener("resize", this.setDivSizeThrottleable);
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
bookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
bookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;


AutoWidthPdf.tsx



import * as React from 'react';
import { Document, Page, pdfjs } from 'react-pdf';

pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;

interface IProps {
file: string;
width: number;
onLoadSuccess: (pdf: any) => void;
}
export class AutoWidthPdf extends React.Component<IProps> {
render = () => (
<Document
file={this.props.file}
onLoadSuccess={(_: any) => this.props.onLoadSuccess(_)}
>
<Page
pageNumber={1}
width={this.props.width}
/>
</Document>
);
}




Update 1: Cancel throttleable function (still no luck)



const DEFAULT_WIDTH = 140;

class Book extends React.Component {
setDivSizeThrottleable: ((() => void) & Cancelable) | undefined;
pdfWrapper: HTMLDivElement | null = null;
state = {
hidden: true,
pdfWidth: DEFAULT_WIDTH,
};

componentDidMount = () => {
this.setDivSizeThrottleable = throttle(
() => {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
},
500,
);

this.setDivSizeThrottleable();
window.addEventListener("resize", this.setDivSizeThrottleable);
};

componentWillUnmount = () => {
window.removeEventListener("resize", this.setDivSizeThrottleable!);
this.setDivSizeThrottleable!.cancel();
this.setDivSizeThrottleable = undefined;
};

render = () => (
<div className="Book">
{ this.state.hidden && <div className="Book__LoadNotification centered">Book is being loaded...</div> }

<div className={this.getPdfContentContainerClassName()}>
<BookCommandPanel
BookTextPath={BookTextPath}
/>

<div className="Book__PdfContent" ref={ref => this.pdfWrapper = ref}>
<AutoWidthPdf
file={BookTextPath}
width={this.state.pdfWidth}
onLoadSuccess={(_: any) => this.onDocumentComplete()}
/>
</div>

<BookCommandPanel
BookTextPath={BookTextPath}
/>
</div>
</div>
);

getPdfContentContainerClassName = () => this.state.hidden ? 'hidden' : '';

onDocumentComplete = () => {
try {
this.setState({ hidden: false });
this.setDivSizeThrottleable!();
} catch (caughtError) {
console.warn({ caughtError });
}
};
}

export default Book;






javascript reactjs typescript lodash setstate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 1:11

























asked Dec 27 '18 at 18:33









Igor Soloydenko

1,94621546




1,94621546












  • Does the problem persist if you comment out the add & remove listeners?
    – ic3b3rg
    Dec 27 '18 at 19:20










  • @ic3b3rg the problem disappears if there's no event listening code
    – Igor Soloydenko
    Dec 27 '18 at 20:26










  • ok, did you try the suggestion to do this.setDivSizeThrottleable.cancel() instead of the this.isComponentMounted guard?
    – ic3b3rg
    Dec 27 '18 at 20:30










  • @ic3b3rg Still the same run-time warning.
    – Igor Soloydenko
    Dec 27 '18 at 23:38


















  • Does the problem persist if you comment out the add & remove listeners?
    – ic3b3rg
    Dec 27 '18 at 19:20










  • @ic3b3rg the problem disappears if there's no event listening code
    – Igor Soloydenko
    Dec 27 '18 at 20:26










  • ok, did you try the suggestion to do this.setDivSizeThrottleable.cancel() instead of the this.isComponentMounted guard?
    – ic3b3rg
    Dec 27 '18 at 20:30










  • @ic3b3rg Still the same run-time warning.
    – Igor Soloydenko
    Dec 27 '18 at 23:38
















Does the problem persist if you comment out the add & remove listeners?
– ic3b3rg
Dec 27 '18 at 19:20




Does the problem persist if you comment out the add & remove listeners?
– ic3b3rg
Dec 27 '18 at 19:20












@ic3b3rg the problem disappears if there's no event listening code
– Igor Soloydenko
Dec 27 '18 at 20:26




@ic3b3rg the problem disappears if there's no event listening code
– Igor Soloydenko
Dec 27 '18 at 20:26












ok, did you try the suggestion to do this.setDivSizeThrottleable.cancel() instead of the this.isComponentMounted guard?
– ic3b3rg
Dec 27 '18 at 20:30




ok, did you try the suggestion to do this.setDivSizeThrottleable.cancel() instead of the this.isComponentMounted guard?
– ic3b3rg
Dec 27 '18 at 20:30












@ic3b3rg Still the same run-time warning.
– Igor Soloydenko
Dec 27 '18 at 23:38




@ic3b3rg Still the same run-time warning.
– Igor Soloydenko
Dec 27 '18 at 23:38












2 Answers
2






active

oldest

votes


















1














Edit: I just realized the warning is referencing a component called TextLayerInternal. That's likely where your bug is. The rest of this is still relevant, but it might not fix your problem.



1) Getting the instance of a component for this warning is tough. It looks like there is some discussion to improve this in React but there currently is no easy way to do it. The reason it hasn't been built yet, I suspect, is likely because components are expected to be written in such a way that setState after unmount isn't possible no matter what the state of the component is. The problem, as far as the React team is concerned, is always in the Component code and not the Component instance, which is why you get the Component Type name.



That answer might be unsatisfactory, but I think I can fix your problem.



2) Lodashes throttled function has a cancel method. Call cancel in componentWillUnmount and ditch the isComponentMounted. Canceling is more "idiomatically" React than introducing a new property.






share|improve this answer























  • Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
    – Igor Soloydenko
    Dec 27 '18 at 20:29










  • Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
    – Igor Soloydenko
    Dec 27 '18 at 23:37



















1














try changing setDivSizeThrottleable to



this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
{ leading: false, trailing: true }
);





share|improve this answer





















  • I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
    – Igor Soloydenko
    Dec 28 '18 at 1:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949393%2fcant-perform-a-react-state-update-on-an-unmounted-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Edit: I just realized the warning is referencing a component called TextLayerInternal. That's likely where your bug is. The rest of this is still relevant, but it might not fix your problem.



1) Getting the instance of a component for this warning is tough. It looks like there is some discussion to improve this in React but there currently is no easy way to do it. The reason it hasn't been built yet, I suspect, is likely because components are expected to be written in such a way that setState after unmount isn't possible no matter what the state of the component is. The problem, as far as the React team is concerned, is always in the Component code and not the Component instance, which is why you get the Component Type name.



That answer might be unsatisfactory, but I think I can fix your problem.



2) Lodashes throttled function has a cancel method. Call cancel in componentWillUnmount and ditch the isComponentMounted. Canceling is more "idiomatically" React than introducing a new property.






share|improve this answer























  • Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
    – Igor Soloydenko
    Dec 27 '18 at 20:29










  • Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
    – Igor Soloydenko
    Dec 27 '18 at 23:37
















1














Edit: I just realized the warning is referencing a component called TextLayerInternal. That's likely where your bug is. The rest of this is still relevant, but it might not fix your problem.



1) Getting the instance of a component for this warning is tough. It looks like there is some discussion to improve this in React but there currently is no easy way to do it. The reason it hasn't been built yet, I suspect, is likely because components are expected to be written in such a way that setState after unmount isn't possible no matter what the state of the component is. The problem, as far as the React team is concerned, is always in the Component code and not the Component instance, which is why you get the Component Type name.



That answer might be unsatisfactory, but I think I can fix your problem.



2) Lodashes throttled function has a cancel method. Call cancel in componentWillUnmount and ditch the isComponentMounted. Canceling is more "idiomatically" React than introducing a new property.






share|improve this answer























  • Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
    – Igor Soloydenko
    Dec 27 '18 at 20:29










  • Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
    – Igor Soloydenko
    Dec 27 '18 at 23:37














1












1








1






Edit: I just realized the warning is referencing a component called TextLayerInternal. That's likely where your bug is. The rest of this is still relevant, but it might not fix your problem.



1) Getting the instance of a component for this warning is tough. It looks like there is some discussion to improve this in React but there currently is no easy way to do it. The reason it hasn't been built yet, I suspect, is likely because components are expected to be written in such a way that setState after unmount isn't possible no matter what the state of the component is. The problem, as far as the React team is concerned, is always in the Component code and not the Component instance, which is why you get the Component Type name.



That answer might be unsatisfactory, but I think I can fix your problem.



2) Lodashes throttled function has a cancel method. Call cancel in componentWillUnmount and ditch the isComponentMounted. Canceling is more "idiomatically" React than introducing a new property.






share|improve this answer














Edit: I just realized the warning is referencing a component called TextLayerInternal. That's likely where your bug is. The rest of this is still relevant, but it might not fix your problem.



1) Getting the instance of a component for this warning is tough. It looks like there is some discussion to improve this in React but there currently is no easy way to do it. The reason it hasn't been built yet, I suspect, is likely because components are expected to be written in such a way that setState after unmount isn't possible no matter what the state of the component is. The problem, as far as the React team is concerned, is always in the Component code and not the Component instance, which is why you get the Component Type name.



That answer might be unsatisfactory, but I think I can fix your problem.



2) Lodashes throttled function has a cancel method. Call cancel in componentWillUnmount and ditch the isComponentMounted. Canceling is more "idiomatically" React than introducing a new property.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 27 '18 at 19:30

























answered Dec 27 '18 at 19:20









R Esmond

713414




713414












  • Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
    – Igor Soloydenko
    Dec 27 '18 at 20:29










  • Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
    – Igor Soloydenko
    Dec 27 '18 at 23:37


















  • Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
    – Igor Soloydenko
    Dec 27 '18 at 20:29










  • Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
    – Igor Soloydenko
    Dec 27 '18 at 23:37
















Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
– Igor Soloydenko
Dec 27 '18 at 20:29




Issue is, I don't directly control TextLayerInternal. Thus, I don't know "who's fault is the setState() call". I'll try the cancel as per your advice and see how it goes,
– Igor Soloydenko
Dec 27 '18 at 20:29












Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
– Igor Soloydenko
Dec 27 '18 at 23:37




Unfortunately, I still see the warning. Please check the code in Update 1 section to verify I'm doing things the right way.
– Igor Soloydenko
Dec 27 '18 at 23:37













1














try changing setDivSizeThrottleable to



this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
{ leading: false, trailing: true }
);





share|improve this answer





















  • I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
    – Igor Soloydenko
    Dec 28 '18 at 1:09
















1














try changing setDivSizeThrottleable to



this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
{ leading: false, trailing: true }
);





share|improve this answer





















  • I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
    – Igor Soloydenko
    Dec 28 '18 at 1:09














1












1








1






try changing setDivSizeThrottleable to



this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
{ leading: false, trailing: true }
);





share|improve this answer












try changing setDivSizeThrottleable to



this.setDivSizeThrottleable = throttle(
() => {
if (this.isComponentMounted) {
this.setState({
pdfWidth: this.pdfWrapper!.getBoundingClientRect().width - 5,
});
}
},
500,
{ leading: false, trailing: true }
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 27 '18 at 23:55









ic3b3rg

10.1k41945




10.1k41945












  • I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
    – Igor Soloydenko
    Dec 28 '18 at 1:09


















  • I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
    – Igor Soloydenko
    Dec 28 '18 at 1:09
















I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
– Igor Soloydenko
Dec 28 '18 at 1:09




I did try it. Now I am consistently seeing the warning which I was only observing time to time on resizing the window before making this change. ¯_(ツ)_/¯ Thanks for trying this though.
– Igor Soloydenko
Dec 28 '18 at 1:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949393%2fcant-perform-a-react-state-update-on-an-unmounted-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas