react-to-print icon indicating copy to clipboard operation
react-to-print copied to clipboard

i new chrome browser iframe and svg d3 charts print is not working and crashing the website

Open mohamedikram-qapita opened this issue 2 years ago • 7 comments

custom hook

import { useRef, useState } from 'react';
import { useReactToPrint, IReactToPrintProps } from 'react-to-print';

export const usePrint = (
  config: IReactToPrintProps = { content: () => null },
): [boolean, any, (() => void) | undefined] => {
  const [isPrinting, setIsPrinting] = useState<boolean>(false);

  const componentRef: any = useRef();

  const handlePrint = useReactToPrint({
    ...config,
    content: () => componentRef?.current,
    onBeforeGetContent: () => setIsPrinting(true),
    onBeforePrint: () => setIsPrinting(false),
  });

  return [isPrinting, componentRef, handlePrint];
};

in the component im calling as

const [isPrinting, componentRef, handlePrint] = usePrint({
    content: () => null,
  });

  const handlePrinter = () => {
    if (handlePrint) {
      handlePrint();
    }
  };

output i have attached image Screenshot from 2022-03-14 13-26-48

mohamedikram-qapita avatar Mar 15 '22 07:03 mohamedikram-qapita

What errors are you getting? Also, calling the state methods in the callbacks will not work as expected. The state methods are asynchronous, and printing might happen before your state effects and/or re-renders happen.

MatthewHerbst avatar Mar 15 '22 07:03 MatthewHerbst

@MatthewHerbst i have made changes after going through examples still page is crashing.kindly check the video i have attached

const componentRef = React.useRef(null);
const reactToPrintContent = React.useCallback(() => {
  return componentRef.current;
}, [componentRef.current]);

const reactToPrintTrigger = React.useCallback(() => {
  return <button>Print using a Functional Component</button>;
}, []);

return (
  <ReactToPrint
    content={reactToPrintContent}
    documentTitle="AwesomeFileName"
    removeAfterPrint
    trigger={reactToPrintTrigger}
  />
  <CustomComponent ref={componentRef} />
)

https://user-images.githubusercontent.com/95607047/158347944-4d823637-c459-40de-a2dc-d6cb43abfda8.mp4

mohamedikram-qapita avatar Mar 15 '22 09:03 mohamedikram-qapita

Hello. Are you getting any errors in the developer console?

MatthewHerbst avatar Mar 16 '22 04:03 MatthewHerbst

no im not getting any error in developer console

https://user-images.githubusercontent.com/95607047/158520328-86228e25-5183-42fa-9c4e-96f1c5f64950.mp4

mohamedikram-qapita avatar Mar 16 '22 05:03 mohamedikram-qapita

Hmm, very strange. Any chance you could make a Codesandbox example of the error so I can debug?

MatthewHerbst avatar Mar 16 '22 05:03 MatthewHerbst

yeah sure i will try to add that things in code sand box

mohamedikram-qapita avatar Mar 16 '22 06:03 mohamedikram-qapita

Hello. Did you solve your problems?

MatthewHerbst avatar Mar 27 '22 22:03 MatthewHerbst