TypeScript is JavaScript with syntax for types
프로그래밍 언어가 작동하기 전에 type을 확인!
설치를 해야겠죠?
npx create-react-app 내 앱 이름 --template typescript
npm i --save-dev @types/styled-components
npm i styled-components
스타일 컴포넌트도 설치해줘요~
하 밥 먹었더니 졸림 서류 제출하고 다시 ㄱㄱ
<Circle bgColor={'teal'} />
자 먼저 app.js에서 이렇게 넘겨줄겁니다.
function Circle({ bgColor }: CircleProps) {
return <Container bgColor={bgColor} />;
}
자 얘는 일단
interface CircleProps {
bgColor: string;
}
const Container = styled.div<CircleProps>`
width: 200px;
height: 200px;
background-color: ${(props) => props.bgColor};
`;
-> 이해 완
있어도 ㄱㅊ 없어도 ㄱㅊ은 props 만들기
props? : type

props?? 값
<Container bgColor={bgColor} borderColor={borderColor ?? bgColor} />
function Circle({ bgColor, borderColor, text = 'default text' }: CircleProps)
저렇게 해도 ㄱㄴ~
state를 만들면 type이 변하는 경우 거의 없음
const [value, setValue] = useState<number | string>(1);

타입스크립트 또또캐~~
form을 만들어봐요
const onChange = (event: React.FormEvent<HTMLInputElement>) => {};
자 그러면 얘는 무엇일까
const {
currentTarget: { value },
} = event;
setValue(value);
const value = event.currentTarget.value
랑 똑같습니다.
그럼 왜? 저렇게 쓸까요?? (강의 댓글 참고함)
currentTarget 안에서 여러개 값을 가져오고 싶을 때
const value = event.currentTarget.value;
const tagName = event.currentTarget.tagName;
const width = event.currentTarget.width;
const id = event.currentTarget.id;
const {
currentTarget : {value, tagName,width, id}
} = event ;
보자마자 이해 완
https://styled-components.com/docs/api#typescript
styled.d.ts 파일 생성하기
import 'styled-components';
declare module 'styled-components' {
export interface DefaultTheme {
textColor: string;
bgColor: string;
btnColor: string;
}
}
export const lighttheme: DefaultTheme = {
bgColor: 'white',
textColor: 'black',
btnColor: 'tomato',
};
<ThemeProvider theme={dartTheme}>
const H1 = styled.h1`
color: ${(props) => props.theme.textColor};
`;