ํ์์ ํ๋๋๋ก React ์ปดํฌ๋ํธ๋ฅผ ํตํด props๋ฅผ ์ ๋ฌํด๋ด ์๋ค.
import styled from "styled-components";
const Container = styled.div`
width: 100px;
height: 100px;
background-color: ${props=>props.bgColor};
`;
const Text = styled.h1``;
function Circle({bgColor}){
return <Container bgColor={bgColor}/>
}
function App() {
return (
<>
<Circle bgColor="tomato"/>
</>
);
}
export default App;
ํ์ ์คํฌ๋ฆฝํธ์์ ์ด๋ฐ์์ผ๋ก ์์ฑํ๋ฉด ๋ ๊ฐ์ง ๋ฌธ์ ์ ์ด ๋ฐ์ํฉ๋๋ค.
1๋ฒ์ ํด๊ฒฐํ๋ ค๋ฉด props์ ํ์ ์ interface๋ฅผ ํตํด ์ ์ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
interface CircleProps {
bgColor: string;
}
function Circle({bgColor}:CircleProps){
return <Container bgColor={bgColor}/>
}
2๋ฒ์ ๊ฒฝ์ฐ TypeScript์๊ฒ bgColor๋ฅผ styled-component์๊ฒ๋ ๋ณด๋ด๊ณ ์ถ๋ค๊ณ ์๋ ค์ค์ผ ํฉ๋๋ค.
๋์ผํ๊ฒ interface๋ฅผ ์์ฑํ๋ฉด ๋์ง๋ง ์ฌ๊ธฐ์ bgColor๊ฐ string ํ์
์ผ๋ก ๋์ผํ๋ฏ๋ก CircleProps๋ฅผ ์ฌ์ฌ์ฉ ํด๋ด
์๋ค.
const Container = styled.div<CircleProps>`
width: 100px;
height: 100px;
background-color: ${props=>props.bgColor};
`;
์ด๋ ๊ฒ ํ๋ฉด ๋ชจ๋ ๊ฒฝ๊ณ ๊ฐ ์ฌ๋ผ์ง๊ณ ํ๋ฉด์ด ๋ ๋๋ง ๋ฉ๋๋ค.
์ด๋ฒ์ ์ปดํฌ๋ํธ ๋๊ฐ๋ฅผ ํตํด ํ๋๋ ์ฌ๊ฐํ์ผ๋ก ํ๋๋ ์์ผ๋ก ์ถ๋ ฅํด๋ด
์๋ค.
๋์ผํ ์ฝ๋๋ก ์ฌ๊ฐํ์ ๋๊ฐ ๋ง๋ค์ด์ฃผ๊ณ , ํ๋๋ ์์ผ๋ก ๊น์๋ด
์๋ค.
function App() {
return (
<>
<Circle bgColor="tomato"/>
<Circle bgColor="teal" borderRadius="100px"/>
</>
);
}
๊ธฐ์กด์ ํ๋ ๊ฒ ์ฒ๋ผ TS์๊ฒ ์๋ก์ด props์ด ๋ค์ด์์ผ๋ ํ์
ํํธ๋ฅผ ์ค์ผ๊ฒ ์ฃ ?
interface์ ์๋ก์ด props์ ํ์
์ ๋ฑ๋กํด์ค๋๋ค.
interface CircleProps {
bgColor: string;
borderRadius: string;
}
๊ทธ๋ฐ๋ฐ ์ด๋ ๊ฒ ํ๋ฉด ์ฒซ ๋ฒ์งธ ๋ํ์์ ์๋ฌ๊ฐ ๋ฉ๋๋ค.
Property 'borderRadius' is missing in type '{ bgColor: string; }' but required in type 'CircleProps'.
"CircleProps์์ borderRadius ํ์
์จ๋๊ณ ์ ์ฌ์ฉ์ํ๋?" ๋ผ๋ ๋ด์ฉ์
๋๋ค.
์ด๋ฅผ ํด๊ฒฐํด์ฃผ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ฌผ์ํ๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋ฉ๋๋ค.
interface CircleProps {
bgColor: string;
borderRadius?: string;
}
์ด๋ ๊ฒ ๋๋ฉด ๊ธฐ์กด์ string ํ์
์ผ๋ก๋ง ์ ์๋ borderRadius์ undefined ๊ฐ ๋ค์ด์๋ ํ์ฉํ๊ฒ ๋ค๋ ๋ป์ผ๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค.
CircleProps.borderRadius?: string | undefined
๋๋ถ์ ๊ฒฝ์ฐ ์ปดํฌ๋ํธ์ Props์ Styled Components์ props๊ฐ ๊ฐ์ง๋ ์์ต๋๋ค.
์ฐ์ต์ ์ํด ๋๊ฐ์ interface๋ฅผ ๋ถ๋ฆฌํด์ค ์ํ๋ก ๋ค์ ์ ์ฒด ์ฝ๋๋ฅผ ๋ด
์๋ค.
import styled from "styled-components";
interface ContainerProps{
bgColor: string;
borderRadius: string;
}
interface CircleProps {
bgColor: string;
borderRadius?: string;
}
const Container = styled.div<ContainerProps>`
width: 100px;
height: 100px;
background-color: ${props=>props.bgColor};
border-radius: ${props=>props.borderRadius};
`;
const Text = styled.h1``;
function Circle({bgColor, borderRadius}:CircleProps){
return <Container bgColor={bgColor} borderRadius={borderRadius}/>
}
function App() {
return (
<>
<Circle bgColor="tomato"/>
<Circle bgColor="teal" borderRadius="100px"/>
</>
);
}
export default App;
๋ง์ฝ ์ด๋ ๊ฒ ์์ฑํ๋ฉด ๋ styled components ๋ถ๋ถ์์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ ์
๋๋ค.
์ ๊ฐ ContainerProps์๋ ?๋ฅผ ์์คฌ๊ฑฐ๋ ์,
interface ContainerProps{
bgColor: string;
borderRadius: string;
}
interface CircleProps {
bgColor: string;
borderRadius?: string;
}
๋ฐฐ์ด์ง ํ๋ฃจ์ฐจ์ธ ์ ์๊ฐ์ด์ง๋ง ? ๊ธฐํธ๋ฅผ ๋จ๋ฐํด์ undefined๋ฅผ ํ์ฉํด์ฃผ๋ ๊ฒ์ ๊ทธ๋ค์ง TS์์ ์งํฅํด์ผ ํ๋ ๋ฐฉ๋ฒ์ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
์ ์ด์ TS๋ฅผ ์ฐ๋ ๋ชฉ์ ์ด ํ์
์ด๋
ธํ
์ด์
์ ํตํด ์ปดํ์ผ ์ ์ ์๋ฌ๋ฅผ ๋ฐฉ์งํ๊ณ ์ ํ๋ ๋ชฉ์ ์ด ํฌ๋๊น์.
๊ทธ๋ ๋ค๋ฉด ์ด ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์์๊น์?
์๋ฌ๊ฐ ๋๋ ๋ถ๋ถ์ ๋ค์ ์ฝ๋์
๋๋ค.
return <Container bgColor={bgColor} borderRadius={borderRadius}/>
borderRadius๊ฐ ํ์ชฝ Container์๋ง ์ฌ์ฉ๋๊ณ ์๋ค๊ณ ์ฐก์ฐก๋๋๊น, default ๊ฐ์ ์ค๋ฒ๋ฆฌ๋ฉด ๋ ๊ฒ ๊ฐ๋ค์.
๋ค์๊ณผ ๊ฐ์ด ๋ฐ๊ฟ๋ด
์๋ค.
<Container bgColor={bgColor} borderRadius={borderRadius ?? "0px"}/>
์ด ์ฝ๋๋ borderRadius๊ฐ์ด ์กด์ฌํ๋ฉด ??์ ์ผ์ชฝ์ธ borderRadius๋ฅผ ์ฐ๊ณ , ์์ผ๋ฉด "0px"๋ก ํ๊ฒ ๋ค ๋ผ๋ ๋ป์ ๋๋ค.
์๋๋ฉด React Props๋ฅผ ์ ๋ฌํ ๋ default ๊ฐ์ ์ฃผ๋ ๋ฐฉ๋ฒ๋ ์ข๊ฒ ๋ค์!
function Circle({bgColor, borderRadius="0px"}:CircleProps){
return <Container bgColor={bgColor} borderRadius={borderRadius}/>
}
๋ ๊ฐ์ง ๋ค ์ข์ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ์ต๋๋ค๐
TypeScript๋ฅผ ๋ฐฐ์ด ์ฒซ๋ ์ธ๋ฐ ๊ต์ฅํ ๋ง์์ ๋ค์์ต๋๋ค.
๊ธฐ์กด์ ํ์ด์ฌ์ ์ฌ์ฉํด์ ๋์ ํ์ดํ์ธ์ด์ ์ต์ํด์ ธ ์๊ธฐ ๋๋ฌธ์ TypeScript๋ฅผ ๋ฐฐ์ฐ๋ ๊ฒ์ด ๊บผ๋ ค์ก๋๋ฐ ์คํ๋ ค JS๋ณด๋ค ํจ์ฌ ์ง๊ด์ ์ด๊ณ ํธํ์ต๋๋ค.
์ ์ด์ JS๋ ์๋ฌ๊ฐ ์์ฒญ ๋ถ์น์ ํ๋๋ฐ TS์ ๊ฒฝ์ฐ ๋งค์ฐ ์น์ ํ๊ฒ ์๋ฌ๋ฅผ ์๋ ค์ฃผ๊ธฐ ๋๋ฌธ์ ๋ด๊ฐ ๋ญ ์๋ชปํ๋์ง ํฐ์นญ๋ฐ๋ ๋๋์ด์์ต๋๋ค.
๊ฒฐ๋ก : TS ์งฑ์งฑ๐