import the necessary libraries as bellow for the .tsx file.
import React, { useRef } from 'react';
import { useReactToPrint } from 'react-to-print';
Create necessary button,
<Button
variant="contained"
color="primary"
onClick={() => handlePrint && handlePrint()} // Wrap the call in an anonymous function
>
Print Profile
</Button>
Creat the function,
const printRef = useRef<HTMLDivElement>(null); // Ref for printable content
const handlePrint = useReactToPrint({
contentRef: printRef,
documentTitle: `Principal_Profile_${ttUserId}`,
onAfterPrint: () => console.log('Printing completed'),
});
After these steps I was success and was able to print the content in the typescript file as I needed.