163 views
Order of console.log output in React component №62
Task condition
Determine the order in which messages will appear in the console during the first render of the component, considering the behavior of hooks useEffect, useLayoutEffect, and their cleanup functions.
import { useEffect, useLayoutEffect, useState } from 'react';
export function App() {
console.log('App');
const [counter, setCounter] = useState(0);
useEffect(() => {
setCounter(prev => prev + 1);
}, []);
useEffect(() => {
console.log('useEffect 1');
return () => {
console.log('useEffect 1, cleanup');
};
}, [counter]);
useEffect(() => {
console.log('useEffect 2');
return () => {
console.log('useEffect 2 cleanup');
};
}, [counter]);
useLayoutEffect(() => {
console.log('useLayoutEffect');
return () => {
console.log('useLayoutEffect cleanup');
};
}, [counter]);
return null;
}