Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use react-to-print with TypeScript?

I usually use react-to-print (https://www.npmjs.com/package/react-to-print) for printing React components with a low effort and great flexibility. I'm starting to write my applications with TypeScript, and this is the first time I need to combine these two things.

This is my code:

<ReactToPrint
  trigger={() => <Button variant="contained" color="primary">Generar</Button>}
  content={() => componentRef.current}
/>
<PrintableComponent ref={componentRef} />

To create the reference, I simply do:

const componentRef = useRef();

In JavaScript, it works, but when I use .tsx, I get an error in the "content" parameter of the ReactToPrint component and another in the ref parameter of my own PrintableComponent. Could someone help me with this?

Basically, the errors say that the interfaces do not match.

like image 342
AxelLeon Avatar asked Apr 03 '20 23:04

AxelLeon


People also ask

Can you use React with TypeScript?

Using TypeScript with Create React AppCreate React App supports TypeScript out of the box. You can also add it to an existing Create React App project, as documented here.

How do I print an image in react JS?

Use onClick event to handle a function that can use to download and print the image.

How to write react in typescript?

When you wish to add TypeScript to a current application, then installing TypeScript and all the necessary types will be important. You may have to rename the files to .ts or .tsx and then begin the server. This will create the tsconfig.json file automatically and you will be ready to write React in TypeScript. 3.

How do I import a page from react-PDF into typescript?

import { Document, Page } from 'react-pdf'; //or const { Document, Page } = require ('react-pdf'); If TypeScript complains about missing declarations, just add a @types folder to your project, create a subfolder inside of it called react-pdf and file inside that folder called index.d.ts with one line:

Should I use typescript or react for IntelliSense?

Developer and author at DigitalOcean. TypeScript is awesome. So is React. Let’s use them both together! Using TypeScript allows us to get the benefits of IntelliSense, as well as the ability to further reason about our code.

How to integrate typescript with create-react-app?

Making a new React app using the create-react-app v2.1 or higher create-react-app 2.1 has now been integrated into the typescript. If you are building a new project using CRA, use— typescript as a standard and the new project will be set up using typescript. 2. Add TypeScript to current create-react-app project


Video Answer


2 Answers

You can define the type for useRef to get rid of the ts error credit to shane935:

const componentRef = useRef<HTMLDivElement>(null)

And if you, like me, are using functional components you will get a problem trying to use that ref following react-to-print class based components. To bypass this error you can wrap your component you wish to print in a div:

<ReactToPrint
  trigger={() => <Button variant="contained" color="primary">Generar</Button>}
  content={() => componentRef.current}
/>
<div ref={componentRef}>
  <PrintableComponent />
</div>

Everything inside this div will be printed.

like image 117
tiny_mike Avatar answered Oct 18 '22 21:10

tiny_mike


Seems like a known issue when using hooks: https://github.com/gregnb/react-to-print/issues/214

As an alternative, you can avoid the useRef hook and follow the example in the source repo which seems to work in TypeScript:

https://github.com/gregnb/react-to-print/blob/master/example/index.tsx

i.e., the first example on the npm readme doc: https://www.npmjs.com/package/react-to-print#example

like image 23
mirage Avatar answered Oct 18 '22 23:10

mirage