-
Notifications
You must be signed in to change notification settings - Fork 0
[동현] Web Component #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: donghyun
Are you sure you want to change the base?
Conversation
src/core/index.ts
Outdated
| @@ -0,0 +1,50 @@ | |||
| interface ComponentOptions<TElement = HTMLElement, Props = any, State = any> { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
타입의 기본값을 명시해준건가요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞아요! 이건 수정되기 전 파일이라서 수정된 파일에는 조금 더 타입을 좁혔습니다!
|
|
||
| export default class TodoList extends Component<HTMLUListElement, TodoListProps> { | ||
| template(): string { | ||
| const { todos } = this.props! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
! 뭔가요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 Non-null 단언 연산자 라고 해서 해당 값이 undefined나 null이 아니라는 뜻입니다
| this.componentDidMount() | ||
| } | ||
|
|
||
| componentDidMount() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 메서드 첨 알았어요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
의미에 맞는진 아직 잘 모르겠네요,, 더 공부를 해봐야 정확하게 판단이 내려질것 같습니다
|
|
||
| componentDidMount() {} | ||
|
|
||
| addEvent(eventType: keyof HTMLElementEventMap, selector: keyof HTMLElementTagNameMap | string, callback: EventListener) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굿
| import Component from "../core" | ||
|
|
||
| export default class TodoInput extends Component<HTMLDivElement> { | ||
| template(): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기도 타입을 지정했군요?? 근데 string 타입인가요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
template은 템플릿 리터럴을 사용하는 것이기 때문에 반환 값을 string 타입으로 한거였어요. 사실 일부러 작성한 것 보다는 기존 Component에 타입이 정의되어 있어서 자동완성할 때 저렇게 되네요!
| export default class TodoInput extends Component<HTMLDivElement> { | ||
| template(): string { | ||
| return ` | ||
| <form> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
프리티어 파일 설정할 때 정렬 관련 속성을 넣으면 읽기 편할 것 같아요ㅠㅠ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수정하겠습니다!
| } | ||
|
|
||
| setEvent(): void { | ||
| this.addEvent("click", "#toggle-button", (event) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아티클엔 클래스로 지정해썼는데 아이디로 바꾼 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
클래스는 어디서든 사용할 수 있도록 하는 목표가 있지만, 여기에선 굳이? 싶어서 아이디로 사용했습니다. 그래도 확장성을 생각한다면 클래스로 가는것이 더 맞겠네요
| @@ -0,0 +1,67 @@ | |||
| type ComponentInternalRecord = Record<string, any> | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 아래 interface에 바로 선언하지 않고 타입을 선언해서 사용한 이유가 뭔가요?
동현님의 인터페이스와 타입을 사용하는 기준이 궁금합니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
인터페이스를 사용하지 않은 이유는 확장을 할 필요가 없기 때문이였어요. 만약 인터페이스를 사용한다면 아래처럼 작성하게 되는데요.
interface ComponentInternalRecord extends Record<string,any>{}이미 Record<string, any>이기 때문에 확장을 하는게 의미가 없다고 생각했어요. 따라서 type을 사용하는게 더 명확하지 않을까? 라는 생각을 했습니다
| > { | ||
| target: TElement | ||
| props?: Props | ||
| private _state?: State |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
불변성을 신경쓴 부분인거죠?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
불변성이라기 보다, 외부에서 값을 접근할 수 없도록 하기 위함입니다!
|
|
||
| addTodo(title: string) { | ||
| const newTodo = { | ||
| id: Date.now(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 신박합니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사실 이거는 굉장히 안좋습니다 ㅋㅋㅋㅋㅋㅋ 왜냐하면 id는 유일해야하는데 겹칠 수 있어요. 이런 것도 더 고민을 해봐야겠네요
고민한 것.
componetDidMount로 변경배운 점.
아티클의 코멘트를 보면 현재는 렌더링 성능이 안좋다고 나와있다. 그래서 개발자 도구를 활용해서 찾아보니, 모든 DOM들이 렌더링되는 문제를 발견했다. 현재 코드는 상태를 변경하면 모든 컴포넌트가 마운트되기 때문이였다. 추후에
Virtual DOM을 만드는 방법을 학습한다면 이 문제를 해결할 수 있을것 같다.현재 코드의 문제점.
논의
내 생각: 클래스는 인스턴스를 생성하기 때문에 해당 컴포넌트의 실행 시점을 제어할 수 있다. 반면에 함수형은 함수가 호출되고 종료되면 소멸되기 때문에 클래스형 컴포넌트와 같이 다양한 생명주기를 갖는건 힘들지 않을까? 라는 추측을 한다.
[2024.12.02] 추후에 논의 더 추가될 수 있음.