In what order do a parent's and a child's Effects run on mount, and clean up on unmount?
Parent renders Child. Both log when they render, when their Effect runs, and when it cleans up. The app isn't in Strict Mode.
import { useEffect, useState } from "react";
function Child() {
console.log("render Child");
useEffect(() => {
console.log("Child effect");
return () => console.log("Child cleanup");
}, []);
return <p>Child</p>;
}
function Parent() {
console.log("render Parent");
useEffect(() => {
console.log("Parent effect");
return () => console.log("Parent cleanup");
}, []);
return <Child />;
}
export default function App() {
const [show, setShow] = useState(true);
return (
<>
{show && <Parent />}
<button onClick={() => setShow(false)}>Hide</button>
</>
);
}
It runs in development, rendered as <App />, and then:
- You click Hide once.
The answer
Work out every line it logs, in order, and what the page shows at the end. Then check.
Checking your sign-in…