-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
window.print() crashes if a 'print' event listener causes a rerender (Chrome, DEV-mode only) #16734
Comments
Thank you for the detailed reproduction steps and code! Seems odd indeed, we'll have a look. |
Same issue on Chrome 72 with traditional components rather than hooks |
Took another approach to the async call and it seems that the print really doesn't work well with async. function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function asyncPrint() {
await sleep(10000);
window.print();
} However, calling |
This is actually a Chromium bug. I ran into it a few months ago and filed https://bugs.chromium.org/p/chromium/issues/detail?id=956832 for it. It should only affect React in development mode, not the production build. That said, React can work around it if we choose. Specifically, this .dispatchEvent call will fail silently in this case and
React could detect that case and fall back to a try/catch implementation for invokeGuardedCallback instead. (Might also make sense to handle the case where .dispatchEvent throws, though it didn't seem that's what's happening here.) Though I was hoping Chromium would just fix the issue so React doesn't need a workaround. |
(cc @stubbornella for the Chrome bug) |
I have come across this same bug. Does anyone have a workaround or will the fix be out soon. |
@aaqibshehzad Unfortunately there's no easy workaround for applications right now. We need to wait for the Chrome bug to be fixed or add a workaround in React. However, this only affects the development build of React. So at least it should work correctly in your production builds. |
* Fix development mode hang when iframe is removed * Also fix #16734
Same issue here, waiting for the fix to be released. |
Hang: setTimeout(() => {
window.print();
}, number); or this.setState(() => ({
xxx: 123
}),
() => {
window.print();
}); Works well: window.print(); |
The fix will likely make it into 17 and not any 16.x release. Although maybe we could backport if there is an urgent common need. This only happens in development mode so I don’t think it is an urgent problem. |
Got it, thanks! |
Actually this is happening for me in deployment mode as well. I threw up a stack overflow question here > https://stackoverflow.com/questions/63428865/change-state-before-and-after-print-for-displaying-only-on-print |
The problem reported in this issue should be fixed in React 17 RC. If you still experience it, please raise a new issue with a reproducing example. |
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
Programmatically calling window.print() can cause React to report strange errors before crashing under certain circumstances. The trigger seems to be a call to print() that results in a React state change somewhere (which, in Chrome, seems to happen because the print preview it shows can cause media query events, which can be hooked up to calls to a setState function). This does not always happen, however--as shown in the example if the code is not called from in a setTimeout it doesn't seem to crash. In addition, if the user initiates printing instead (e.g. via CTRL+P), React never crashes.
The two errors I've seen happen as a result of this are
Failed to execute 'handleEvent' on 'EventListener': The provided callback is no longer runnable
(needs "Pause on caught exceptions" in Chrome's DevTools to catch), and after a few of those they're followed byMaximum update depth exceeded
(even though the setState function is only called once). Once this happens, there's a good chance the tab become completely unresponsive after the print dialog is closed.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.
A minimal example can be found here: https://codesandbox.io/s/immutable-snowflake-06cj2 . There are no external dependencies. The code is commented with instructions to reproduce the behavior.
I've also included a copy of the code here for completeness' sake:
What is the expected behavior?
React should not throw errors after calling window.print(), even if doing so causes state changes.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
This is the latest release (16.9.0). I am unsure if other versions of React are affected.
The behavior was tested using Chrome 76 on Windows 10. This behavior will not happen in Firefox and likely other browsers as well, probably due to the unique way Chrome handles printing and print previews.
The text was updated successfully, but these errors were encountered: