a-x- / react-easy-print

Powerful react component for easy printing
https://www.npmjs.com/package/react-easy-print
MIT License
87 stars 19 forks source link

Prevent print-only components from rendering when we aren't in the 'print mode'. #39

Open alex-gokkun opened 5 years ago

alex-gokkun commented 5 years ago

Correct me if I'm wrong but I haven't found an official way for a print-only component not being rendered. Let's say that I have a component that depends on a plethora of props. Each prop change would trigger a re-render. It's a waste of effort if I'm not actually printing anything at the moment. By subscribing to beforeprint and afterprint events I got a workaround but I cannot say I'm satisfied with it. It seems to violate separation of conerns. Are there any plans on supporting this in the library?

a-x- commented 5 years ago

Hm, official way is there. Let me a second

a-x- commented 5 years ago

Did you try <Print printOnly...?

alex-gokkun commented 5 years ago

Yes, I did. It prevents the component from being displayed but doesn't prevent it from being rendered.

a-x- commented 5 years ago

I got now. Thank you for the case!

There is a reason for this behavior. If PrintProvider would re-render the children (whole components tree) state of components will be lost.

I thinked a few minutes right now and realized the solution: PrintProvider should triggers target re-renderings for printOnly and some another components Through pubsub in the context (Like ‘redux’ or ‘material ui’ theming does it in its connect/withStyles hocs).

Do you wanna try to implement this?

I can try in a 2-4 next days.

AustinMutschler commented 4 years ago

Any update on this?