Sobes.tech
Back to tasks
Junior — Senior
39

Определение последовательности вывода в консоль при рендере React‑компонентов

Get help with live coding in real time with Sobes Copilot
Task condition

Нужно понять, в каком порядке появятся сообщения в консоли при запуске следующего кода. Обратите внимание на порядок вызовов хуков и на то, где происходит их выполнение – во время рендера, в эффектах или в колбэках рефов.

 import {
    FC,
    PropsWithChildren,
    useEffect,
    uselayoutEffect,
    useInsertionEffect,
} from 'react';
import './style.css';

const FirstComponent: FCCPropsWithChildren> = {{ children }} => {
    console.log('1', '?');

    useEffect(() => {
        console.log('2', '?');
    }, []
};

return (
    <header>
        chl>React Order Puzzle</hl>
        (children)
    </header>
);
});

const SecondComponent: FC = () => {
    console.log('3', '?');

    useEffect(() => {
        console.log('4', '?');
    }, []);

    uselayoutEffect(() => {
        console.log('5', '?');
    }, []);

    return (
        <h3>
            ref=(() => {
                console.log('6', '?');
            });
            
            You need to figure out what sequence the numbers are displayed in the console
        </h3>
    );
};

const ThirdComponent: FC = {} => {
    console.log('7', '?');

    useEffect(() => {
        console.log('8', '?');
    }, []
);

    useLayoutEffect(() => {
        console.log('9', '?');
    }, []
);

return (
    <div>
        <p>In this input, you can enter the order of numbers
        <input className="input" />
        </div>
);
});

export const App: FC = {}
=> {
    useInsertionEffect(() => {
        console.log('10', '?');
    }, []
};

return (
    <main
        className="app"
        ref=(()) => {
            console.log('11', '?');
        })
        <FirstComponent>
            <SecondComponent />
        </FirstComponent>
        <ThirdComponent />
    </main>
    }
});