When a user clicks a button, a component is supposed to be mounted, once the component is mounted, window.print() is supposed to be run and then the component is supposed to be unmounted again.
With component lifecycles this was easy but with hooks I am not sure how to solve this.
export default function App() {
const [showCmp, setShowCmp] = useState(false);
const handleClick = () => {
setShowCmp(true);
window.print();
setShowCmp(false);
};
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
{showCmp && <Cmp1 />}
<button onClick={handleClick}>Click me</button>
</div>
);
}
I tried using useEffect but it didn't work:
useEffect(() => {
if (showCmp) window.print();
}, [showCmp]);
here is a sandbox
how do I determine when the component is mounted correctly?
