-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl(lazy-initialize): set content for handlePrint() and handleClick(…
…) via second optional argument using [content] (#679)
- Loading branch information
Showing
5 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"typescript.tsdk": "./node_modules/typescript/lib" | ||
"typescript.tsdk": "./node_modules/typescript/lib", | ||
"typescript.enablePromptUseWorkspaceTsdk": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
examples/FunctionalComponentWithHookAndLazyContent/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from "react"; | ||
|
||
import { ComponentToPrint } from "../ComponentToPrint"; | ||
import { useReactToPrint } from "../../src/index"; | ||
import { CUSTOM_FONTS } from "../fonts"; | ||
|
||
export const FunctionalComponentWithHookAndLazyContent = () => { | ||
const componentRef = React.useRef(null); | ||
|
||
const onBeforeGetContentResolve = React.useRef<(() => void) | null>(null); | ||
|
||
const [loading, setLoading] = React.useState(false); | ||
const [text, setText] = React.useState("Some cool text from the parent"); | ||
|
||
const handleAfterPrint = React.useCallback(() => { | ||
console.log("`onAfterPrint` called"); // tslint:disable-line no-console | ||
}, []); | ||
|
||
const handleBeforePrint = React.useCallback(() => { | ||
console.log("`onBeforePrint` called"); // tslint:disable-line no-console | ||
}, []); | ||
|
||
const handleOnBeforeGetContent = React.useCallback(() => { | ||
console.log("`onBeforeGetContent` called"); // tslint:disable-line no-console | ||
setLoading(true); | ||
setText("Loading new text..."); | ||
|
||
return new Promise<void>((resolve) => { | ||
onBeforeGetContentResolve.current = resolve; | ||
|
||
setTimeout(() => { | ||
setLoading(false); | ||
setText("New, Updated Text!"); | ||
resolve(); | ||
}, 2000); | ||
}); | ||
}, [setLoading, setText]); | ||
|
||
const reactToPrintContent = React.useCallback(() => { | ||
return componentRef.current; | ||
}, [componentRef.current]); | ||
|
||
const handlePrint = useReactToPrint({ | ||
documentTitle: "SuperFileName", | ||
onBeforeGetContent: handleOnBeforeGetContent, | ||
onBeforePrint: handleBeforePrint, | ||
onAfterPrint: handleAfterPrint, | ||
removeAfterPrint: true, | ||
fonts: CUSTOM_FONTS | ||
}); | ||
|
||
React.useEffect(() => { | ||
if (text === "New, Updated Text!" && typeof onBeforeGetContentResolve.current === "function") { | ||
onBeforeGetContentResolve.current(); | ||
} | ||
}, [onBeforeGetContentResolve.current, text]); | ||
|
||
return ( | ||
<div> | ||
{loading && <p className="indicator">onBeforeGetContent: Loading...</p>} | ||
<button onClick={() => handlePrint(null, reactToPrintContent)}> | ||
Print using a Functional Component with the useReactToPrint hook without `content` option | ||
</button> | ||
<ComponentToPrint ref={componentRef} text={text} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters