6.
How do you invoke the setStatus function only when the component mounts, using React hooks?
function MyComponent(props) {
const [status, setStatus ] = useState(false);
return <h1>Done: {status}</h1>;
}
componentDidMount(() => {
setStatus(true);
});
useEffect(() => {
setStatus(true);
});
useEffect((status) => {
setStatus(true);
}, []);
useEffect(() => {
setStatus(true);
}, []);
Correct: D
In order to invoke the setDone function only when the component mounts, you can use the useEffect hook with an empty dependency array []. This ensures that the effect is only run once, when the component is mounted. Using useEffect without a dependency array or with a non-empty dependency array, will result in the effect being run on every component update. The componentDidMount lifecycle method, is not applicable to functional components using hooks.