ํฐ์คํ ๋ฆฌ ๋ทฐ
TS
React ์ปดํฌ๋ํธ ๊ฐ์ props ๋ฐ์ดํฐ ์ ๋ฌ ๊ณผ์ ์์์ type error
choi95 2022. 3. 26. 20:21๋ฌธ์
์์ ์ปดํฌ๋ํธ ๋ด์์ ํ์ ์ปดํฌ๋ํธ๋ก props๋ฅผ ์ ๋ฌํ๋ ๊ณผ์ ์์ type ์๋ฌ๊ฐ ๋ฐ์ํ์๋ค.
export interface InfoType {
[key: string]: any;
id: number;
title: string;
client: string;
due: string;
count: number;
amount: number;
method: string[];
material: string[];
status: string;
}
const InfoListForm: FC<OptionalProps> = ({ method, material, status }) => {
( ... )
return (
<Container isEmpty={isEmpty}>
{isEmpty ? (
<GuideLine>
<FrontInformation>์กฐ๊ฑด์ ๋ง๋ ๊ฒฌ์ ์์ฒญ์ด ์์ต๋๋ค.</FrontInformation>
</GuideLine>
) : (
currentInfoList.map((info: InfoType, index) => {
return <CardBoard key={info.id} info={info} index={index} />;
})
)}
</Container>
);
};
'{ key: number; info: InfoType; index: number; }' ํ์์ 'InfoType' ํ์์ id, title, client, due ์ธ 5๊ฐ ์์ฑ์ด ์์ต๋๋ค.
ts(2740)
๋ฌธ์ ํด๊ฒฐ
interface InfoType ์์ฒด๋ฅผ ์ธํฐํ์ด์ค๋ก ์ฌ์ฉํ๋ ค๊ณ ๋ฐ์ ํ ๋ฌธ์ ์๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด props๋ก ๋๊ฒจ์ฃผ๋ ๋ฐ์ดํฐ๋ฅผ ์คํ๋ ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ๊ฐ๋ณ์ ์ธ ๊ฐ๋ค์ ๋ชฉ๋ก์ผ๋ก ๋ง๋ ๋ค ํ์ ์ปดํฌ๋ํธ ์ธ์ ๋ด์์ ํ๋์ ๊ฐ์ฒด ๊ฐ์ผ๋ก ๋ฐ์ ๋์คํธ๋ญ์ณ๋ง ํ ๋น์ ํตํด ๊ฐ ๊ฐ๋ค์ ์ฌ์ฉํ์๋ค.
const InfoListForm: FC<OptionalProps> = ({ method, material, status }) => {
( ... )
return (
<Container isEmpty={isEmpty}>
{isEmpty ? (
<GuideLine>
<FrontInformation>์กฐ๊ฑด์ ๋ง๋ ๊ฒฌ์ ์์ฒญ์ด ์์ต๋๋ค.</FrontInformation>
</GuideLine>
) : (
currentInfoList.map((info: InfoType, index) => {
return <CardBoard key={info.id} {...info} index={index} />;
})
)}
</Container>
);
};
const CardBoard: FC<InfoType> = (info: InfoType) => {
const { title, client, due, count, amount, method, material, status, index } = info;
( ... )
};