React-to-print: Clear onload in all browsers (not just IE)?

Created on 2 Jun 2020  路  3Comments  路  Source: gregnb/react-to-print

Hi! This library is great, thanks so much for making it! 馃挅

Is there a need to only set printWindow.onload = null; in IE? Seems like that could run regardless of browser right?

https://github.com/gregnb/react-to-print/blob/a192abd07cda318b89d7eaf81198c985cde58b97/src/index.tsx#L228

Some backing context for why I'd like to remove that line: https://codesandbox.io/s/react-to-print-infinite-onloads-h4mnr infinitely hangs when running tests.

  • Enzyme continuously calls the printWindow's onload, even though it's only been set once.
  • That causes the act in the test to never complete...

    • ...but the onBeforeLoad function never gets called if you use a synchronous act.

You can verify this by cloning the sandbox locally, prettifying the compiled index.js, and adding console logs.

console.log("Setting onload");
f.onload = function() {
  console.log("Calling onload");
    window.navigator && window.navigator.userAgent.indexOf("Trident/7.0") > -1 && (f.onload = null);
console.log
    Setting onload
      at e.handlePrint (node_modules/react-to-print/lib/index.js:109:35)

  console.log
    Calling onload
      at HTMLIFrameElement.f.onload (node_modules/react-to-print/lib/index.js:111:37)

  console.log
    Calling onload
      at HTMLIFrameElement.f.onload (node_modules/react-to-print/lib/index.js:111:37)

  console.log
    Calling onload
      at HTMLIFrameElement.f.onload (node_modules/react-to-print/lib/index.js:111:37)
...

Now, this seems like a bug in Enzyme and should probably be fixed there (https://github.com/enzymejs/enzyme/issues/2400), _but_ it would be fixed by this already-seemingly-positive change.

Downstream bug

All 3 comments

Hi, thanks for the report. That does seem like an interesting issue. And I agree, I don't see why we can't just always clear out the event listener. I'll get it in with some changes I'm making tonight!

Should be fixed in the just published 2.9.0, let me know if you see any issues.

Also, if you ever wanted to help us move from tslint to eslint, would appreciate any support there :)

Aaaaah I would love to! Expect a PR tonight! 馃尮

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MikeSha picture MikeSha  路  4Comments

404sand808s picture 404sand808s  路  7Comments

k-jiang picture k-jiang  路  5Comments

erangia picture erangia  路  7Comments

icebravo picture icebravo  路  5Comments