Skip to content

Instantly share code, notes, and snippets.

@Drag13
Last active October 2, 2023 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drag13/77763f642b022945f6135141cf3c5aeb to your computer and use it in GitHub Desktop.
Save Drag13/77763f642b022945f6135141cf3c5aeb to your computer and use it in GitHub Desktop.
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