Skip to content
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

[Step 6] Router 직접 만들기 (김채은) #30

Open
wants to merge 27 commits into
base: chchaeun
Choose a base branch
from

Conversation

chchaeun
Copy link

@chchaeun chchaeun commented Mar 9, 2023

🎯 완료한 Task

  • HistoryRouter

피드백

학습 시간

  • 이번 주에 학습에 투자한 시간 →
  • 학습에 집중될 때 나는 어떤 상태였는가 →
  • 집중되지 않을 땐 어떤 상태였는가 →

몰입

  • 학습 하면서 좋았던 점 →
  • 학습 하면서 아쉬웠던 점 →

미션

  • 미션 진행 과정에서 어려움을 겪었던 부분 →
  • 미션에서 개선되었으면 하는 점 →

Copy link
Member

@JunilHwang JunilHwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

점점 코드가 풍요로워지고 있군요 ㅎㅎ
좋습니다 👍👍👍

Comment on lines 1 to 33
export const frozen = (value) => {
let frozenValue;
if (Array.isArray(value)) {
frozenValue = [];
frozenArray(frozenValue, value);
} else if (typeof value === "object" && value !== null) {
frozenValue = {};
frozenObject(frozenValue, value);
} else {
frozenValue = value;
}
return frozenValue;
};

const frozenArray = (frozenValue, arr) => {
arr.forEach((v, i) => {
if (typeof v === "object" && v !== null) {
frozenValue[i] = frozen(v);
} else {
frozenValue[i] = v;
}
});
};

const frozenObject = (frozenValue, obj) => {
for (const [k, v] of Object.entries(obj)) {
if (typeof v === "object" && v !== null) {
frozenValue[k] = frozen(v);
} else {
frozenValue[k] = v;
}
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

객체를 변경하지 못하게 만드는 함수가 아니라, 객체를 복사하는 함수 같아요!
그렇다면 frozen이 아니라 deepCopy 처럼 표현하면 어떨까요?

Comment on lines 20 to 27
case ADD_TODO:
return { ...state, todos: action.payload };
case DELETE_TODO:
return { ...state, todos: action.payload };
case TOGGLE_TODO:
return { ...state, todos: action.payload };
case EDIT_TODO:
return { ...state, todos: action.payload };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하나로 합칠 수 있지 않을까요?

Comment on lines 18 to 23
get state() {
return frozen({ ...this.#state });
}
get props() {
return frozen({ ...this.#props });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구조해제할당은 하지 않아도 똑같이 동작할 것 같아요 ㅋㅋ

Comment on lines 50 to 52
events.forEach((event) => {
this.#addEvent(event.type, event.target, event.handler);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
events.forEach((event) => {
this.#addEvent(event.type, event.target, event.handler);
});
events.forEach(({ type, target, handler }) => this.#addEvent(type, target, handler));

사람마다 선호도의 차이가 있겠지만, 저는 이렇게 표현하는게 좋은 것 같아요! 더 깔끔한 느낌?

Comment on lines 46 to 49
const events = this.event();
if (!events) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const events = this.event();
if (!events) {
return;
}
const events = this.event() || [];

이러면 한 방에 해결되지 않을까요!?

Comment on lines 9 to 12
for (const [key, value] of Object.entries(newState)) {
if (!state.hasOwnProperty(key)) continue;
state[key] = value;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 로직도 유틸함수로 만들어서 관리할 수 있겠네요!


renderPath();

window.addEventListener("locationChange", renderPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locationChange라는 이벤트 타입도 상수로 관리할 수 있지 않을까 싶어요~

Comment on lines 9 to 11
window.addEventListener("popstate", (e) => {
this.#setUrl(e.target.location.pathname);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pathname만 가져다가 사용하게 되면 queryString이나 hash 같은건 무시되지 않을까요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/ko/docs/Web/API/URL/URL

위의 API를 활용하면 조금 더 편리하게 router에 대한 정보를 관리할 수 있답니다!

Comment on lines 19 to 34
#parse(url) {
const splitUrl = url.split("?");
const path = splitUrl[0];
const queryString = {};

if (splitUrl.length > 1) {
splitUrl[1].split("&").forEach((query) => {
const [key, value] = query.split("=");
if (key && value) {
queryString[key] = value;
}
});
}

return { path, queryString };
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앞선 리뷰를 참고해주세요~


const locationChange = new CustomEvent("locationChange");

export default class HistoryRouter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashRouter도 만들어보시는걸 추천드려요 ㅋㅋ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants