return () => code입력
import { useState, useEffect } from "react";
function Hello() {
useEffect(() => {
console.log("created!");
return () => console.log("destroyed!"); // component가 사라질 때 동작하는 cleanup 함수.
}, []);
return <h1>Hello</h1>
}
function App() {
const [showing, setShowing] = useState(false);
const onClick = () => setShowing((prev) => !prev);
return (
<div>
{showing ? <Hello /> : null}
<button onClick={onClick}>{showing ? "Hide" : "Show"}</button>
</div>
);
}
export default App;