So you've created a React component and would love to give end users the ability to print out the contents of that component. This package aims to solve that by popping up a print window with CSS styles copied over as well. Show
DemoInstallimport React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }9 API<ReactToPrint />The component accepts the following props: NameTypeDescriptionimport React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }0 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }1One or more class names to pass to the print window, separated by spaces import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }2 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3A function that returns a component reference value. The content of this reference value is then used for print import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }4 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }5Copy all import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }6 and import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }7 tags from import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }8 inside the parent window into the print window. (default: import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }9) import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); };0 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }1Set the title for printing when saving as a file import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); };2 import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); };3You may optionally provide a list of fonts which will be loaded into the printing iframe. This is useful if you are using custom fonts import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); };4 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3Callback function that triggers after the print dialog is closed regardless of if the user selected to print or cancel import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); };6 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3Callback function that triggers before the library gathers the page's content. Either returns void or a Promise. This can be used to change the content on the page before printing import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); };8 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3Callback function that triggers before print. Either returns void or a Promise. Note: this function is run immediately prior to printing, but after the page's content has been gathered. To modify content before printing, use import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };0 instead import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };1 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3Callback function (signature: `function(errorLocation: 'onBeforePrint' import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };3 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }1 or import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3We set some basic styles to help improve page printing. Use this to override them and provide your own. If given as a function it must return a import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }1 import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };7 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3If passed, this function will be used instead of import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };9 to print the content. This function is passed the const componentRef = useRef(null);0 which is the iframe used internally to gather content for printing. When finished, this function must return a Promise. Use this to print in non-browser environments such as Electron const componentRef = useRef(null);1 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }5Remove the print iframe after action. Defaults to const componentRef = useRef(null);3 const componentRef = useRef(null);4 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }5When passed, prevents const componentRef = useRef(null);6 logging of errors const componentRef = useRef(null);7 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3A function that returns a React Component or Element. Note: under the hood, we inject a custom const componentRef = useRef(null);9 prop into the returned Component/Element. As such, do not provide an const componentRef = useRef(null);9 prop to the root node returned by <div className="parent"> <p>Hello</p> </div>1, as it will be overwritten <div className="parent"> <p>Hello</p> </div>2 import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }1Set the nonce attribute for whitelisting script and style -elements for CSP (content security policy) <div className="parent"> <p>Hello</p> </div>4If you need extra control over printing and don't want to specify <div className="parent"> <p>Hello</p> </div>1 directly, <div className="parent"> <p>Hello</p> </div>4 allows you to gain direct access to the <div className="parent"> <p>Hello</p> </div>7 method which triggers the print action. Requires React >=16.3.0. See the examples below for usage. <div className="parent"> <p>Hello</p> </div>8For functional components, use the <div className="parent"> <p>Hello</p> </div>8 hook, which accepts an object with the same configuration props as div.parent p { color:red; }0 and returns a <div className="parent"> <p>Hello</p> </div>7 function which when called will trigger the print action. Requires React >=16.8.0. See the examples below for usage. Compatibilitydiv.parent p { color:red; }2 should be compatible with most major browsers. We also do our best to support IE11. Mobile Browsers in WebViewWhile printing on mobile browsers should work, printing within a WebView (when your page is opened by another app such as Facebook or Slack, but not by the full browser itself) is known to not work on many if not all WebViews. Some don't make the correct API available. Others make it available but cause printing to no-op when in WebView. We are actively researching resolutions to this issue, but it likely requires changes by Google/Chromium and Apple/WebKit. See #384 for more information. If you know of a way we can solve this, your help would be greatly appreciated. Known Incompatible Browsers
Examples// Using a class component, everything works without issue export class ComponentToPrint extends React.PureComponent { render() { return ( <div>My cool content here!</div> ); } } // Using a functional component, you must wrap it in React.forwardRef, and then forward the ref to // the node you want to be the root of the print (usually the outer most node in the ComponentToPrint) // https://reactjs.org/docs/refs-and-the-dom.html#refs-and-function-components export const ComponentToPrint = React.forwardRef((props, ref) => { return ( <div ref={ref}>My cool content here!</div> ); }); Calling from class componentsimport React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } } Calling from class components with <div className="parent"> <p>Hello</p> </div>4import React from 'react'; import ReactToPrint, { PrintContextConsumer } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint content={() => this.componentRef}> <PrintContextConsumer> {({ handlePrint }) => ( <button onClick={handlePrint}>Print this out!</button> )} </PrintContextConsumer> </ReactToPrint> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } } Calling from functional componentsimport React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); return ( <div> <ReactToPrint trigger={() => <button>Print this out!</button>} content={() => componentRef.current} /> <ComponentToPrint ref={componentRef} /> </div> ); }; Calling from functional components with useReactToPrintimport React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); }; Note (401): In TypeScript, if you encounter div.parent p { color:red; }5 error such as: div.parent p { color:red; }6, add div.parent p { color:red; }7 inside the div.parent p { color:red; }8: const componentRef = useRef(null); Known Issues
Common Pitfalls
FAQCan div.parent p { color:red; }2 be used to download a PDF without using the Print Preview window?No. We aren't able to print a PDF as we lose control once the print preview window opens. However, it should be very easy to use div.parent p { color:red; }2 to take the information you need an pass it to a library that can generate a PDF. const handlePrint = useReactToPrint({ ..., print: async (printIframe: HTMLIframeElement) => { // Do whatever you want here, including asynchronous work await generateAndSavePDF(printIframe); }, }); For examples of how others have done this, see #484 Can the <div style={{ display: "none" }}><ComponentToPrint ref={componentRef} /></div>0 be a functional component?Yes, but only if you wrap it with import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }05. div.parent p { color:red; }2 relies on refs to grab the underlying DOM representation of the component, and functional components . Why does div.parent p { color:red; }9 fire even if the user cancels printingdiv.parent p { color:red; }9 fires when the print dialog closes, regardless of why it closes. This is the behavior of the import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }09 browser event. Why does div.parent p { color:red; }2 skip import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }11 tagsimport React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }12s with empty import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }13 attributes are . In addition, they can cause all sorts of undesirable behavior. For example, many browsers - including modern ones, when presented with import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }14 will attempt to load the current page. Some even attempt to load the current page's parent directory. Note: related to the above, import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }15 tags with empty import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }16 attributes are also invalid, and we may not attempt to load them. How do you make <div style={{ display: "none" }}><ComponentToPrint ref={componentRef} /></div>0 show only while printingIf you've created a component that is intended only for printing and should not render in the parent component, wrap that component in a import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }18 with style set to import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }19, like so: <div style={{ display: "none" }}><ComponentToPrint ref={componentRef} /></div> This will hide <div style={{ display: "none" }}><ComponentToPrint ref={componentRef} /></div>0 but keep it in the DOM so that it can be copied for printing. Setting state in import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };0Recall that setting state is asynchronous. As such, you need to pass a import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }22 and wait for the state to update. import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }0 Note: for Class components, just pass the import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }23 to the callback for import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }24: import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }25 Changing print settings in the print dialogUnfortunately there is no standard browser API for interacting with the print dialog. All div.parent p { color:red; }2 is able to do is open the dialog and give it the desired content to print. We cannot modify settings such as the default paper size, if the user has background graphics selected or not, etc. Printing import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }27 elementsdiv.parent p { color:red; }2 tries to wait for import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }27 elements to load before printing but a large part of this is up to the browser. Further, the image displayed will usually be the first frame of the video, which might not be what you expect to show. To ensure the proper image is displayed in the print we highly recommend setting the attribute of the import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }27, which allows specifying an image to be a placeholder for the video until the video loads. Electrondiv.parent p { color:red; }2 can be used for printing in Electron, but you will need to provide your own import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }33 method since Electron does not natively support the import React, { useRef } from 'react'; import { useReactToPrint } from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; const Example = () => { const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current, }); return ( <div> <ComponentToPrint ref={componentRef} /> <button onClick={handlePrint}>Print this out!</button> </div> ); };9 method. Please see this answer on StackOverflow for how to do this. There is a fully-working example of how to use div.parent p { color:red; }2 with Electron available here. Helpful Style TipsSet the page orientationWhile you should be able to place these styles anywhere, sometimes the browser doesn't always pick them up. To force orientation of the page you can include the following in the component being printed: import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }1 Set the page sizeThe default page size is usually A4. Most browsers do not allow JavaScript or CSS to set the page size. For the browsers that do, it is usually done using the CSS page import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }36 property. Check import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }37 to see if the browsers you develop against support this. import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }2 Set custom margin to the page (29)To set custom margin to the page, First, create a function to return the page margin, import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }3 Now, within the JSX call this function within the style tags, import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }4 PS: This style tag should be inside the component that is being passed in as the content ref. Set landscape printing (240)In the component that is passed in as the content ref, add the following: import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }5 Printing elements that are not displayed (159)Instead of using import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }38, try using import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }39 Using the import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }40 propThe import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }40 prop should be a CSS string. For example: import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }42 Getting a blank page when printingMany have found setting the following CSS helpful. See #26 for more. import React from 'react'; import ReactToPrint from 'react-to-print'; import { ComponentToPrint } from './ComponentToPrint'; class Example extends React.PureComponent { render() { return ( <div> <ReactToPrint trigger={() => { // NOTE: could just as easily return <SomeComponent />. Do NOT pass an `onClick` prop // to the root node of the returned component as it will be overwritten. return <a href="#">Print this out!</a>; }} content={() => this.componentRef} /> <ComponentToPrint ref={el => (this.componentRef = el)} /> </div> ); } }6 When you've set the |