Depending on the browser mechanism, there are different answers to this problem. To solve these problems in a simple cross-browser way, I created https://github.com/noderaider/print .
I suggest two npm packages:
Use without reaction
Install via npm :
npm i -S print-utils
HTML:
<iframe id="print-content" src="/frame.html"></iframe>
JavaScript (ES2015)
import { usePrintFrame } from 'print-utils' const disposePrintFrame = usePrintFrame(document.getElementById('print-frame')) disposePrintFrame()
Use with React
npm i -S react-focus
Application:
import React, { Component } from 'react' import reactFocus from 'react-focus' const Focus = reactFocus(React) export default class App extends Component { render() { return ( <div> <div> <h2>Welcome to react-focus</h2> </div> <div> <Focus src={`?d=${Date.now()}`} /> </div> </div> ) } }
cchamberlain
source share