๊ธฐ๋ณธ์ ์ธ ํ์
์คํฌ๋ฆฝํธ์ ๋ํ ์ค๋ช
์ ๋ณ๋์ ํฌ์คํ
์ ํตํด ์ ๋ฆฌ๋ฅผ ํ์ต๋๋ค.
์ฌ๊ธฐ์๋ ๋ฆฌ์กํธ์์ ํ์
์คํฌ๋ฆฝํธ๋ฅผ ์ด๋ป๊ฒ ์ ์ฉ ํ ์ ์๋์ง ๊ณต๋ถํ๊ณ ์ ๋ฆฌ ํฉ๋๋ค! + ์คํ์ผ ์ปดํฌ๋ํธ ์ ์ฉ
์ค์น โ
1. ์ฒ์๋ถํฐ CRA + ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ํ ๋
npx create-react-app my-app --template typescript
or
yarn create react-app my-app --template typescript
2. ํ๋ก์ ํธ ์ค๊ฐ์ ์ค์นํ๋ ๋ฐฉ๋ฒ
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
or
yarn add typescript @types/node @types/react @types/react-dom @types/jest
- ๋ฌธ์
- ๊ทธ ํ, .js ํ์ผ์ -> .tsx ๋ก ๋ฐ๊ฟ๋๋ค.
- ๋ง์ฝ, style-component ๊ฐ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ฐ์ ๋ค์ด์ง ๋ชปํด ์๋ฌ๋ฅผ ๋ด๊ณ ์๋ค๋ฉด, ์๋์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅ ํฉ๋๋ค.
npm i --save-dev @types/styled-components
or
yarn add @types/styled-components
โ Type the Props
App.tsx
import Circle from "./Circle";
function App() {
return (
<div>
<Circle bgColor={'teal'}/>
<Circle bgColor={'tomato'}/>
</div>
);
}
export default App;
Circle.tsx
import styled from "styled-components"
const Container = styled.div`
`;
const Circle = (bgColor) => {
return (
<Container/>
)
}
export default Circle;
- ์์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด, ํ์ ์คํฌ๋ฆฝํธ๋ bgColor ์ ํ์ ์ ์ ์๊ฐ ์์ด ์๋ฌ๋ฅผ ๋ํ๋ ๋๋ค.
interface
๊ฐ์ฒด์ ๋ชจ์์ TypeScript ์๊ฒ ์ค๋ช ํด์ฃผ๋ ๊ฐ๋ , ์ด์ ์๋ ์๋์ ๊ฐ์ ์ฝ๋ ํ์์ผ๋ก ๋ง์ด ์ฌ์ฉ ํ์ต๋๋ค.
const x = (a:number, b:number) => a + b;
- TypeScript ์๊ฒ ๋ณ์ a, b ์ ํ์ ์ ์ค๋ช ํด ์ฃผ๊ณ ์์ต๋๋ค.
interface ์ฌ์ฉ๋ฒ
import styled from "styled-components"
const Container = styled.div`
`;
interface CircleProps {
// TypeScript ์๊ฒ bgColor ๋ ๋ฌธ์์ด์ด๋ผ๊ณ ์๋ฆผ
bgColor : string;
}
const Circle = (bgColor : CircleProps) => {
return (
<Container bgColor={bgColor}/>
)
}
export default Circle;
- ํ์ง๋ง ์์ง๋ ์๋ฌ๊ฐ ๋ฐ์ ํฉ๋๋ค
<Container bgColor={bgColor}/>
bgColor ๋ถ๋ถ์์ - TypeScript ๊ฐ ๋ดค์ ๋๋ Cotainer ๊ฐ div ์ ๋๋ค.
- TypeScript ์๊ฒ bgColor ๋ฅผ styled-component ์๊ฒ ๋ณด๋ด๊ณ ์ถ๋ค๊ณ ์ ๋ฌํด์ผ ํฉ๋๋ค.
โ style-component ์ ๋ณด๋ผ interface ์์ฑ
import React from "react";
import styled from "styled-components"
interface ContainerProps {
bgColor : string;
}
const Container = styled.div<ContainerProps>`
width : 200px;
height : 200px;
background-color : ${(props) => props.bgColor};
`;
interface CircleProps {
// TypeScript ์๊ฒ bgColor ๋ ๋ฌธ์์ด์ด๋ผ๊ณ ์๋ฆผ
bgColor : string;
}
const Circle = ({bgColor} : CircleProps) => {
return (
<React.Fragment>
<Container bgColor={bgColor}/>;
</React.Fragment>
)
}
export default Circle;
โ interface ์ฐ์ต
interface PlayerShape {
name : string;
age : number;
}
const sayHello = (playerObj : PlayerShape) =>
`Hello ${playerObj.name} you are ${playerObj.age} years old`;
sayHello({name:'hyunho' , age : 26});
sayHello({name:'hyunho' , age : '26'}); // ์๋ฌ๊ฐ ๋ฐ์ ํฉ๋๋ค.
- ๋ ๋ฒ์งธ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ์ด์ ๋ age ์ ํ์ ์ string ์ด ์๋๋ผ number ์ ๋๋ค.
- ์ด๋ ๋ฏ interface ๋ ์ฝ๋ ์คํ '์ ' ์ ํ์ธํด์ ์๋ฌ๋ฅผ ๋ง์ ์ค๋๋ค.
์ฐธ๊ณ
- ๋ ธ๋ง๋์ฝ๋
'๐ Front-End > React.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React + TypeScript(state) (0) | 2022.10.26 |
---|---|
React + TypeScript(Optional Props) (0) | 2022.10.26 |
useInfiniteQuery ํธ์ถ (0) | 2022.10.26 |
React Query - ๋ฌดํ ์คํฌ๋กค(Infinite Scroll) (0) | 2022.10.26 |
React Query - ๋ณ์ด(Mutation) (0) | 2022.10.25 |