16.
How can you ensure that the useEffect hook runs only once after the initial render?
useEffect(() => {
// Code to be executed after initial render
}, []);
useEffect(() => {
// Code to be executed after every render
// ...
}, ());
useEffect(() => {
// Code to be executed after specific state or prop changes
// ...
}, {});
useEffect(() => {
// Code to be executed before unmounting the component
// ...
});
Correct: A
The correct answer is A. By passing an empty dependency array as the second argument to the useEffect hook, you can ensure that the effect runs only once after the initial render. This is because the empty dependency array tells React that the effect has no dependencies and therefore, it only needs to be executed once.