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

"TypeError: Cannot read property 'setState' of null" when using React.lazy #29004

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/react/src/ReactLazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ function lazyInitializer<T>(payload: Payload<T>): T {
// as still uninitialized and try again next time. Which is the same as what
// happens if the ctor or any wrappers processing the ctor throws. This might
// end up fixing it if the resolution was a concurrency bug.
let componentIsMounted = true;
thenable.then(
moduleObject => {
if (payload._status === Pending || payload._status === Uninitialized) {
if (componentIsMounted && (payload._status === Pending || payload._status === Uninitialized)) {
// Transition to the next state.
const resolved: ResolvedPayload<T> = (payload: any);
resolved._status = Resolved;
resolved._result = moduleObject;
}
},
error => {
if (payload._status === Pending || payload._status === Uninitialized) {
if (componentIsMounted && (payload._status === Pending || payload._status === Uninitialized)) {
// Transition to the next state.
const rejected: RejectedPayload = (payload: any);
rejected._status = Rejected;
Expand All @@ -84,6 +85,10 @@ function lazyInitializer<T>(payload: Payload<T>): T {
pending._status = Pending;
pending._result = thenable;
}
// Add a cleanup function to set componentIsMounted to false when the component unmounts
return () => {
componentIsMounted = false;
};
}
if (payload._status === Resolved) {
const moduleObject = payload._result;
Expand Down