Comments (8)
from react-to-print.
Hey there. I'm not sure I fully understand your concern. Are you trying to avoid showing all the pages in the print preview window opened by the browser? I don't think that's possible to my knowledge, though it's generally pretty performant.
In terms of generating the content, you can pass a Promise to the onBeforeGetContent
prop so that will cause react-to-print to wait to print until you resolve the promise, and during that time you can generate the temporary content for printing that may not display on the main page.
Does that answer your question?
from react-to-print.
from react-to-print.
Ah. onBeforeGetContent
is what you want I think. It would let you only render what you want to print when the print is happening, that way you don't need to have it rendered and hidden when not printing. Does that make sense?
from react-to-print.
from react-to-print.
Ah.
onBeforeGetContent
is what you want I think. It would let you only render what you want to print when the print is happening, that way you don't need to have it rendered and hidden when not printing. Does that make sense?
how would you use this? like you still needs to pass the ref to the content property right?
Edit: Is this how it's supposed to be used
function App() {
const [print, setPrint] = useState(false);
const printRef = useRef<HTMLDivElement | null>(null);
return (
<div>
{print && <Template ref={printRef} />}
<ReactToPrint
trigger={() => <button>Print</button>}
content={() => printRef.current}
onBeforeGetContent={() => {
setPrint(true);
return Promise.resolve();
}}
onAfterPrint={() => {
setPrint(false);
return Promise.resolve();
}}
/>
</div>
);
}
from react-to-print.
That's basically right! Recall that setting state is async though, so it's a bit more complicated to know when the state has actually been set:
function App() {
const [print, setPrint] = useState(false);
const onBeforeGetContentResolveRef = useRef();
const printRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
// Wait for the state update
if (onBeforeGetContentResolveRef.current && print === true) {
onBeforeGetContentResolveRef.current(); // Resolve the promise
onBeforeGetContentResolveRef.current = undefined;
}
}, [print])
return (
<div>
{print && <Template ref={printRef} />}
<ReactToPrint
trigger={() => <button>Print</button>}
content={() => printRef.current}
onBeforeGetContent={() => {
setPrint(true);
return new Promise((resolve) => {
onBeforeGetContentResolveRef.current = resolve;
});
}}
onAfterPrint={() => {
setPrint(false);
}}
/>
</div>
);
}
In the old world you could do this with the callback function from seState
that runs after the state is applied, but they didn't add that into the functional component/hook world unfortunately
from react-to-print.
@AlejandroSanchez90 circling back here, did the above work for you?
from react-to-print.
Related Issues (20)
- Page break wide content HOT 3
- Superscripts <sup/> and subscripts <sub/> HOT 1
- react-to-print custom print html content dev vs prod discrepancy HOT 4
- [BUG] [TEMPORARY FIX] - Print container on Chrome + MUI is hiding `Header and Footers` option as default behavior HOT 5
- The FOOTER to only display at the LAST PAGE HOT 3
- codesandbox examples are not loading HOT 2
- onAfterPrint is called before print iframe appears HOT 1
- On mobile devices are not truncated accordingly ref HOT 1
- Add Border to every Page HOT 4
- Add dynamic colors to print function HOT 1
- How to check that the user click on Save in `onAfterPrint`? HOT 2
- getting Supplied Data is not a valid base64-String jsPDF.convertStringToImageData erorr while directly downloading image HOT 1
- Border top and bottom HOT 1
- Save as PDF hangs in print view HOT 1
- Blank PDF when trying to generate in production. HOT 2
- Why does it render different content when in im browsing in mobile HOT 3
- Blank Page while printing after a certain period of time HOT 9
- How to add style using PageStyle prop, without it overriding component’s style HOT 5
- Warning: findDOMNode is deprecated outside StrictMode. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-to-print.