import React from 'react'
export default function ListRender() {
const fruits = ['apple', 'banana','cacao'];
const listItems = fruits.map((item, index) => {
return(
<h3>{item}</h3>
)
});
// value: listItems = [<h3>apple</h3>, <h3>banana</h3>, <h3>cacao</h3>]
return (
<div>
{listItems}
</div>
)
}
축약 코드
const listItems = fruits.map((item, index) => {
return(
<h3>{item}</h3>
)
});
//map 한줄 코드 조건에 따른 정리
const listItems = fruits.map((item, index) => <h3>{item}</h3>);
key={ value }const listItems = fruits.map((item, index) => <h3 key={index}>{item}</h3>);(Key O) -> Map 사용
import React from 'react'
export default function ListRender() {
const counts:number[] = [];
for(let count = 0 ; count < 10; count++){
counts.push(count);
}
return (
<div>
{counts.map(item => <h5 key={item}>반복작업</h5>)}
</div>
)
}
(Key X) -> Array 사용
const counts2 = new Array(10).fill(0);
import React from 'react'
export default function ListRender() {
const employees = [
{name: '홍길동', department: '재무'},
{name: '이영희', department: '영업'},
{name: '김철수', department: '재무'},
{name: '이성계', department: '개발'}
];
const fiEmployees = employees.filter(item => item.department === '재무');
return (
<div>
{fiEmployees.map((item,index) => <h5 key={index}>{`이름: ${item.name} 부서: ${item.department}`}</h5>)}
</div>
)
}
employees.filter(item => item.department === '재무');을 통해 조건을 지정 map를 통해 불러온다