I’ve been trying to use the ReactToPrint library to make a print button that makes it possible to print everything inside a div. In console I receive the errors:
- "react-to-print" did not receive a
contentRef
op…or a
optional-content param pass to its callback." - "There is nothing to print".
When console logging componentRef.current I receive undefined in console, so it’s clear that it doesn’t access the ref, but I can’t figure out why? Any suggestions?
This is my component:
import React, { useRef } from 'react';
import { useReactToPrint } from 'react-to-print';
import { HeaderText } from '../HeaderText/HeaderText';
export const StartPage = () => {
const componentRef = useRef();
const handlePrint = useReactToPrint({
content: () => componentRef.current,
});
console.log(componentRef.current)
return (
<>
<div ref={componentRef}>
<HeaderText />
</div>
<button onClick={handlePrint}>Print</button>
</>
)
};
2
Answers
Make the ref with null:
Use the ref attribute:
Full Version:
If there are still issues it might be a package problem
Have you found out a solution for it? I am facing the same issue.