MatthewHerbst / react-to-print

Print React components in the browser. Supports Chrome, Safari, Firefox and EDGE
MIT License
2.15k stars 220 forks source link

Warning : findDOMNode is deprecated #748

Closed pyleglise closed 1 month ago

pyleglise commented 1 month ago

Hi ! I have a project using react-to-print and I constantly have this warning : Warning: findDOMNode is deprecated and will be removed in the next major release. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-find-node It appears only when I click on the button that fires 'useReactToPrint'.

"react-to-print": "^3.0.1",

Any solution ?

MatthewHerbst commented 1 month ago

Hey! Starting with version 3.0.0 we removed findDOMNode from our code, specifically to stop that warning and be React 19 compatible. Could you please share some of your code? Are you potentially calling findDOMNode?

pyleglise commented 1 month ago

Hi ! I saw the upgrade and I couldn't figure out why I had this error as I'm not using it in my code. Now, I can't share code right now because I broke everything trying to make the v3.0.1 work in my situation. You see, I need to print a single element in a list of elements. So the 'print' button is on each header of the element to be printed and I therefore, the contentRef is a variable... I'm trying to figure out what's the best way to do that. Still working on it. I'll let you know.

Thanks for your answer anyway.

pyleglise commented 1 month ago

Hi there !

Finally got to work in v3.0.1 successfully. No more findDomNode error... Must have been a cache issue. Sorry for false warning. You can close.

And thanks again for this work :-)

MatthewHerbst commented 1 month ago

Glad you got it working! Please let me know if you run into any additional issues. Also, your use-case sounds like a good candidate for our lazy content (example) option, where you don't need to know the ref value when you call the hook, only when you actually go to print