Closed keyboy21 closed 2 days ago
Hmm. Similar to what you were doing in the onBeforePrint
, I wouldn't expect setting state and then immediately printing to work well since setting state is async but you aren't waiting to ensure the state has been set before printing.
Here is a working version of the above: https://codesandbox.io/p/sandbox/react-to-print-beta-state-bug-forked-yc4w4j
(Really appreciate you testing out the beta version, THANK YOU!)
Yeah, since version 2 it works well and i'm use onBeforePrint only for applying styles. Please make your codesandbox version public, i have error: Sandbox not found
Ah, try now, apologies, I always forget to make them public
Image scenario that, we need select data to print before print, it will call print function on every set state changes (useEffect hell). I know we can solve this problem create another state to collect selected data, but it would be great it works out of box (like onBeforePrint to wait dom changes)
Not sure I understand. In onBeforePrint
to do it properly you also need to wait for the state change to set and only then resolve the Promise.
I noticed that onBeforePrint not works
I believe I see the issue, I will play around with it more tonight, thanks for your patience
I just published v3.0.0-beta-2
which should resolve these issues. Please let me know if you are still running into problems, thank you!
I tested version 3.0.0-beta-3, but the issues remain unresolved. Unfortunately, your fix is also not working as expected. When I attempted to run it, I encountered the following error in the browser console:
Unchecked runtime.lastError: The message port closed before a response was received.
For a minimal reproduction code example.
You haven't taught react-to-print
about the data
state, so it doesn't know to wait to print until that is set. Here is a working example: https://codesandbox.io/p/sandbox/react-to-print-beta-3-forked-mp2qv7?file=%2Fsrc%2FApp.tsx%3A54%2C22
To reproduce:
Package versions: "react": "19.0.0-rc-8b08e99e-20240713", "react-dom": "19.0.0-rc-8b08e99e-20240713", "react-to-print": "^3.0.0-beta-1",
Minimal reproduction code
Prev related issue: #706