diff --git a/README.md b/README.md index a184c6b..23c47a0 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ Print the content of a React component. ```tsx const contentRef = useRef(null); -const handlePrint = useReactToPrint({ contentRef }); +const reactToPrintFn = useReactToPrint({ contentRef }); return (
- +
Content to print
); diff --git a/examples/BasicComponent/index.tsx b/examples/BasicComponent/index.tsx index ce56c91..3573c14 100644 --- a/examples/BasicComponent/index.tsx +++ b/examples/BasicComponent/index.tsx @@ -27,13 +27,10 @@ export const BasicComponent = () => { onBeforePrint: handleBeforePrint, }); - const handleOnClick = React.useCallback(() => { - printFn(); - }, [printFn]); - return (
- + {/* @ts-expect-error Works without lazy content wrapping */} +
); diff --git a/src/hooks/useReactToPrint.ts b/src/hooks/useReactToPrint.ts index 99eea42..324fe1b 100644 --- a/src/hooks/useReactToPrint.ts +++ b/src/hooks/useReactToPrint.ts @@ -41,14 +41,6 @@ export function useReactToPrint(options: UseReactToPrintOptions): UseReactToPrin return; } - if (!contentNode) { - logMessages({ - messages: ['"react-to-print" could not locate the DOM node corresponding with the `content` prop'], - suppressErrors, - }); - return; - } - // NOTE: `canvas` elements do not have their painted images copied // https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode const clonedContentNode = contentNode.cloneNode(true); diff --git a/src/utils/getContentNode.ts b/src/utils/getContentNode.ts index daf7ea2..79f1ee0 100644 --- a/src/utils/getContentNode.ts +++ b/src/utils/getContentNode.ts @@ -18,7 +18,12 @@ export function getContentNode({ contentRef, optionalContent, suppressErrors }: }); } - return optionalContent(); + // This check allows passing the callback from `useReactToPrint` directly into event + // handlers without having to wrap it in another function to capture the event + // See [#742](https://github.com/MatthewHerbst/react-to-print/issues/742) and [#724](https://github.com/MatthewHerbst/react-to-print/issues/724) + if (typeof optionalContent === "function") { + return optionalContent(); + } } if (contentRef) {