I'm trying to print the content of a function in react but getting
Uncaught TypeError: Cannot read property 'innerHTML' of null
function:
export default function Home(props) {
return (
<h4>Some other contents</h4>
<button className='btn' type='button' onClick={printDiv('printMe')}>Print</button>
)
}
function Data(props) {
return (
<div id='printMe'>
<p>Some contents in here returning from my database</p>
<p>On an html table</p>
</div>
)
}
function printDiv(divName) {
let printContents = document.getElementById(divName).innerHTML;
let originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
So far, once the application is loaded I get the error. I'm not sure what's causing the issues.
onClickhas to be a function; what happens if you pass a function? LikeonClick={() => printDiv('printMe')}.