I am using react-to-print to print bills in my billing-system app. I've encountered an issue where one of the states (rows), containing a list of bills, is updated just before printing to consolidate the items entered in the bill. Although the state is updated successfully, the print output does not reflect the changes.
MyComponent.jsx
const handleBillPrint = useReactToPrint({
content: () => printableRef.current,
onAfterPrint: () => handleBillSave("success") && handleBillReset(),
onBeforeGetContent: () => setRows(consolidateItemRows(rows))
})
I would appreciate any insights or solutions on why the updated state is not being reflected in the print output. Thank you!
Recall that state updates are asynchronous. This means that the
setRowscall does not complete immediately. To fix this, you need to pass a Promise toonBeforeGetContentand then detect that the rows have updated to finally resolve it. There is a README section (reproduced below) about this.