(1-1) 상수 데이터를 사용하는 이유
반복적으로 만들어지는 UI를 일일히 하드코딩을 하게 되면 코드의 가독성이 떨어지고 유지보수 측면에도 좋지 않는 영향을 끼치게 된다. 여기서 상수 데이터를 활용하게 될시에는 Javascript의 Array.map 메서드를 활용해서 반복되는 UI를 보다 간결하게 만들 수 있다.
상수 데이터에서 수정이 필요할경우 레이아웃은 map 메서드의 return 문의 JSX 부분만, 내용은 상수 데이터 부분만 수정하면 되기에 유지보수 및 관리에 용이하다는 장점이 있다.
import React from 'react';
const Signup = () => {
return (
<div>
<select className="month">
<option>1월</option>
<option>2월</option>
<option>3월</option>
<option>4월</option>
<option>5월</option>
</select>
...
<select className="phoneNumber">
<option>010</option>
<option>011</option>
<option>016</option>
<option>017</option>
<option>018</option>
<option>019</option>
</select>
</div>
);
};
export default Signup;
3-1) 상수 데이터 선언
UPPER_SNAKE_CASE naming convention에 따라 명명하는 것이 좋다.내,외부 컴포넌트 작성)
// 배열로만 이용해서 상수 데이터를 선언할경우
const BIRTHDAY_MONTH_LIST = Array.from({length: 12}, (_, i) => `${i + 1}월`);
const PHONENUMBER_LIST = ['010', '011', '016', '017', '018', '019'];
// 배열과 객체 둘다 이용해서 상수 데이터를 선언할경우
const FOOTER_INFO_LIST = [
{ id: 1, link: "https://github.com/terms", text: "Terms" },
{ id: 2, link: "https://github.com/privacy", text: "Privacy" },
...
{ id: 11, link: "https://github.com/about", text: "About" },
];
3-2) 상수 데이터 적용
import React from 'react';
const Signup = () => {
return (
<div>
<select className="birthdayBox month">
{BIRTHDAY_MONTH_LIST.map((month, index) => (
<option key={index}>{month}</option>
))}
</select>
...
<select className="phoneNumber">
{PHONENUMBER_LIST.map((number, index) => (
<option key={index}>{number}</option>
))}
</select>
</div>
);
};
export default Signup;
Prop을 이용하여 상수 데이터 넘겨주기)
// Footer.js
import React from 'react';
const Footer = () => {
return (
<footer>
{/* 생략 */}
<ul>
{FOOTER_INFO_LIST.map(info => (
<FooterInfo key={info.id} link={info.link} text={info.text} />
))}
</ul>
</footer>
);
};
export default Footer;
3-3) 상수 데이터 선언 위치
(1) 내부 컴포넌트에서 선언
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import UserInput from '../../Components/UserInput';
import UserButton from '../../Components/UserButton';
import './Signup.scss';
const Signup = () => {
... 생략 ...
return (
<div className="signup">
... 생략 ...
<div className="numberSelectFrame">
<select className="numberBox">
{PHONENUMBER_LIST.map((number, index) => (
<option key={index}>{number}</option>
))}
</select>
... 생략 ...
</div>
</div>
... 생략 ...
<div className="birthdaySelectFrame">
... 생략 ...
<select className="birthdayBox monthBox">
{BIRTHDAY_MONTH_LIST.map((month, index) => (
<option key={index}>{month}</option>
))}
</select>
... 생략 ...
</div>
</div>
... 생략 ...
</div>
</div>
</div>
);
};
export default Signup;
// 생일 데이터 : 년,월,일
const BIRTHDAY_MONTH_LIST = Array.from({ length: 12 }, (_, i) => `${i + 1}월`);
// 휴대폰 데이터 : (010)
const PHONENUMBER_LIST = ['010', '011', '016', '017', '018', '019'];
1) 컴포넌트의 state나 prop 등등 컴포넌트 리랜더링시 변하는 값을 포함하는 상수 데이터는 컴포넌트 내부에서 작성
2) 컴포넌트가 리랜더링 될 때마다 새로 선언되고 할당될 필요가 없는 상수 데이터는 컴포넌트 외부에서 작성
(2) 별도의 JSX 파일로 분리
// data.jsx
export const FOOTER_INFO_LIST = [
{ id: 1, link: "https://github.com/terms", text: "Terms" },
{ id: 2, link: "https://github.com/privacy", text: "Privacy" },
...
{ id: 11, link: "https://github.com/about", text: "About" },
];
// Footer.js
import React from 'react';
import { FOOTER_INFO_LIST } from './data';
const Footer = () => {
return (
<footer>
{/* 생략 */}
<ul>
{FOOTER_INFO_LIST.map(info => (
<li key=`footerInfo${info.id}`>
<a href={info.link}>{info.text}</a>
</li>
))}
</ul>
</footer>
);
};
export default Footer;
전체 요약)