diegomura / react-pdf

📄 Create PDF files using React
https://react-pdf.org
MIT License
14.65k stars 1.16k forks source link

usePdf triggers re-generating all pdf documents previously generated by another components #1776

Open gitdast opened 2 years ago

gitdast commented 2 years ago

I have multiple components using usePdf to generate pdf on user click. But once some is already ready (download link is displayed) and user clicks on another - all instances regenerate.

Example:

export default function Doc1PdfGenerator (props: IDoc1Props) {
    const [instance] = usePDF({ document: <Doc1PdfDocument {...props} /> });
    const href = instance.url ?? undefined;

    return (
        <>
            <a className={`${instance.loading || instance.error ? 'disabled' : ''}`} href={href} target='_blank'>Open pdf</a>

            {instance?.error && <span>PDF file unavailable</span>}
            {instance?.loading && !instance.error && <span>Creating PDF file ...</span>}
        </>
    )
}

export default function Doc2PdfGenerator (props: IDoc2Props) {
    const [instance] = usePDF({ document: <Doc2PdfDocument {...props} /> });
    ...
}

The first component added to the page - doc1 is generated. 2nd component added to the page - doc2 is generated and doc1 is also regenerated.

gitdast commented 2 years ago

I have tried BlobProvider, usePdf, ... all options and found await pdf(<DocPdfDocument {...props} /> is the only way how to generate multiple documents.

nkroe commented 1 month ago

@diegomura Hi, is there any update on this?