[react] 동적값 출력 및 활용

Yeong·2024년 8월 1일
0
const reactDesc = ['Fundamental', 'Crucial', 'Core'];

function getRandomInt(max) {
    return Math.floor(Math.random() * (max + 1));
}



function Header() {
    const description = reactDesc[getRandomInt(2)];

    return (
        <header>
            <img src="src/assets/react-core-concepts.png" alt="Stylized atom"/>
            <h1>React Essentials</h1>
            <p>
                {reactDesc[getRandomInt(2)]} 
            </p>
            <p>
            	{description}
            </p>
        </header>
    );
}

여기에서 p태그 내부에서 {reactDesc[getRandomInt(2)]} 라고 적어주어도 되지만 가독성을 위하여 return 바깥에서 변수를 지정하여 작성해주는 것이 좋다.

0개의 댓글

관련 채용 정보