๐ props๋ก ๋๊ฒจ์ฃผ๋ data๊ฐ ์ด๋ค ํ์ ์ธ์ง ์ง์ ๋์ง ์์์๋

import React from "react";
import logo from "./logo.svg";
import "./App.css";
import Store from "./Store";
let data = {
name: "type Store",
category: "Korean Food",
address: {
city: "Seoul",
detail: "someWhere",
zipCode: 12345678,
},
menu: [
{ name: "rice", price: 1000 },
{ name: "soup", price: 2000 },
],
};
const App: React.FC = () => {
return (
<div className="App">
<Store **data={data}**/>
</div>
);
};
export default App;
src - model - ํ์ผ๋ช .ts ์์ฑ
App.tsx์ ์๋ data์ ํ์ ๋ค์ ๋ณ๋๋ก ๋นผ์์ type์ ์ง์ ํด์ค๋ค.
ํ์ ์ ์ง์ ํ๋ ๋ฐฉ๋ฒ์๋ type๊ณผ interface ๋ ๊ฐ์ง๊ฐ ์๋ค. ๋์ ์ฐจ์ด์ ์ ํฌ๊ฒ ์์ผ๋, method์ syntax์ ์ฐจ์ด๊ฐ ์๋ค.
โ๏ธ
let data = {
name: "type Store",
category: "Korean Food",
address: {
city: "Seoul",
detail: "someWhere",
zipCode: 12345678,
},
menu: [
{ name: "rice", price: 1000 },
{ name: "soup", price: 2000 },
],
};
type์ผ๋ก ์ง์ ํด๋ณด๊ธฐ
๋ด๊ฐ ์ดํดํ๋ฐ๋ก๋ ์ด๋ค data๋ฅผ ๋ฐ์์ค๋ ๊ฐ์ฒด์ ๊ตฌ์กฐ๋ฅผ ๋ฏธ๋ฆฌ ์ก์๋๊ณ ๊ทธ ์์ ๋ฐ์์ค๋ data๋ค์ type์ ์ ํด๋๋๊ฒ ๊ฐ๋ค.
export type Restaurant = {
name: string;
category: string;
address: {
city: string;
detail: string;
zipCode: number;
};
menu: {
name: string;
price: number;
}[];
};
โ๏ธ type์ ์กฐ๊ธ ๋ ์ฌ์ฌ์ฉ์ฑ ๋ํ๋๋ก ๋ง๋ค์ด๋ณด๊ธฐ
export type Restaurant = {
name: string;
category: string;
address: Address;
menu: Menu[];
};
export type Address = {
city: string;
detail: string;
zipCode: number;
};
export type Menu = {
name: string;
price: number;
};
**import { Restaurant } from "./model/restaurant";**
.
.
let **data:Restaurant** = {
.
.
};