You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm building a PDF report functionality whereby:
a user will click "Download Report" that triggers an async function
The async function fetches data from an API
Updates data state
Open PDF in new tab
The problem is that there's a race condition between updating data state and opening the url in a new tab. The very first time the page loads and user clicks on the button, PDF will be empty. The second click will populate the PDF though.
The following is a simplified version of the code :
import{Page,Text,View,Document,Font,StyleSheet,BlobProvider,}from"@react-pdf/renderer";constMyDocument=(props)=>(<Documenttitle="report.pdf"><Pagesize="A4"style={styles.page}><Viewstyle={styles.letterHead}><Textstyle={styles.title}>Report title</Text><Text>Some date</Text></View>{props.children}</Page></Document>);constOrders=(props)=>{const[orderSummary,setOrderSummary]=useState([]);constdownloadReport=async(url)=>{constreport=awaitreports.get();constorder= ... // transform JSON into View and Text with the datasetOrderSummary(order);window.open(url,"_blank");};return(<Fragment>
...
<BlobProviderdocument={<MyDocumentchildren={orderSummary}/>}>{({ url })=>{return(<divclassName="button"onClick={()=>downloadReport(url)}>
Download Report
</div>);}}</BlobProvider>
...
</Fragment>);
and put updateInstance in useEffect with the data state as dependency but it still only renders the data from second click onward - i.e. first click after page load is always blank.
I saw other discussions here and here, but I don't want to download the file immediately. I want users to be able to read it and download it only if necessary.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, I'm building a PDF report functionality whereby:
The problem is that there's a race condition between updating data state and opening the url in a new tab. The very first time the page loads and user clicks on the button, PDF will be empty. The second click will populate the PDF though.
The following is a simplified version of the code :
I also tried using
and put updateInstance in useEffect with the data state as dependency but it still only renders the data from second click onward - i.e. first click after page load is always blank.
I saw other discussions here and here, but I don't want to download the file immediately. I want users to be able to read it and download it only if necessary.
I'd appreciate any help on this one. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions