배열안의 5개의 리스트에서 하나를 클릭하면 클릭한 하나의 메뉴가 색이 변하면서 List가 필터링되는 기능
//MemberList.jsx
const MemberList = ({ handleSelectedMember }) => {
const [selectedMember, setSelectedMember] = useState("");
const handleMemberClick = (memberName) => { -1
setSelectedMember(memberName);
handleSelectedMember(memberName);
};
return (
<div>
<ListUl>
{members.map((member) => (
<ListLi
key={member}
onClick={() => handleMemberClick(member)}
className={selectedMember === member ? "selected" : ""} -2
>
{member || "All"}
</ListLi>
))}
</ListUl>
</div>
);
};
//Home.jsx
const [selectMember, setSelectMember] = useState("");
const handleSelectedMember = (memberName) => {
setSelectMember(memberName);
};
<SendList selectMember={selectMember} letterList={letterList} />
위에 memberList에서 매개변수로 handleSelectedMember(선택된 멤버)를 보낸다면 가져온 값을 selectMember라는 useState에 담아서 리스트로 보낸다
//SendList.jsx
const filteredList = selectMember ------ 1번
? letterList.filter((item) => item.selectBox === selectMember)
: letterList;
const handleDetail = (list) => { ------ 2번
navigate(`/detail/${list.id}`, { state: list });
};
return (
<>
<CardBox>
{filteredList.length > 0 ? ( ------ 3번
filteredList.map((list) => {
return (
<Card onClick={() => handleDetail(list)} key={list.id}>
<Nickname>{`닉네임 : ${list.nickname}`}</Nickname>
<CardContext>{`내용 : ${list.context}`}</CardContext>
<CardDate>
{`등록시간 : ${new Date(list.createdAt).toLocaleDateString( 4번
"ko",
{
year: "2-digit",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
}
)}`}
</CardDate>
<SelectBox>{`수신인: ${list.selectBox}`}</SelectBox>
</Card>
);
})
) : (
<span>등록된 팬 레터가 없습니다. 등록해주세요</span>
)}
</CardBox>
</>
);
};
export default SendList;