Last active
October 2, 2023 08:28
-
-
Save Drag13/77763f642b022945f6135141cf3c5aeb to your computer and use it in GitHub Desktop.
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
import { useEffect, useState } from 'react'; | |
export default Page; | |
function Page() { | |
const [visible, setVisible] = useState(true); | |
const hideCounter = () => setVisible(false); | |
return ( | |
<main> | |
<button type="button" onClick={hideCounter}> | |
Hide counter | |
</button> | |
<div>{visible && <Counter />}</div> | |
</main> | |
); | |
} | |
function Counter() { | |
const [counter, setCounter] = useState(1); | |
const increment = () => setCounter(counter + 1); | |
useEffect(() => { | |
return () => console.log('Counter unmounted'); | |
}, []); | |
useEffect(() => { | |
return () => console.log('Counter changed', counter); | |
}, [counter]); | |
return <button onClick={increment}>{counter}</button>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment