react-new-window icon indicating copy to clipboard operation
react-new-window copied to clipboard

Open window in print mode

Open rivkiroten opened this issue 6 years ago • 4 comments

Hi, I was wondering if it's possible to open a new window in print mode, with a print dialog box, using this library. Thanks,

rivkiroten avatar Dec 11 '18 07:12 rivkiroten

I needed this same thing, but there's an escape hatch you can use. The <NewWindow/> component attaches the window instance a class member called window. To call the print method you can do the following:

  • Attach a ref to your instance of the <NewWindow/> component.
  • If you are using a class component, then in your componentDidUpdate method you can call the print method like so: windowRef.window.print()
  • If your are using a function component with hooks, attach a ref to the <NewWindow/> component, then create a useEffect function like so:
useEffect(() => {
  if(windowRef.current){
    windowRef.current.window.print()
  }
}, [windowRef.current])

chrisjpatty avatar Dec 16 '18 08:12 chrisjpatty

not working for me @chrisjpatty

Valerika avatar Jan 23 '19 08:01 Valerika

@Valerika i used @chrisjpatty 's hints this way:

<NewWindow ref={ref => (this.myRef = ref)}>
  <SomeComponent newWindowRef={this.myRef} />
</NewWindow>

And within <SomeComponent />:

handlePrint = () => {
  this.props.newWindowRef.window.print()
}

kevinch avatar May 24 '19 19:05 kevinch

There is another way. <NewWindow/> has on undocumented property onOpen that is passed a handle to the window. The following worked for me

<NewWindow onOpen={win=>win.print()} >
  <SomeComponent />
</NewWindow>

AidanNichol avatar Jun 06 '20 09:06 AidanNichol

The onOpen prop was added to the docs.

rmariuzzo avatar Nov 27 '22 21:11 rmariuzzo