-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat(react): useScrollTo 훅 추가 #285
Conversation
🦋 Changeset detectedLatest commit: cdb9ad7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #285 +/- ##
==========================================
+ Coverage 96.85% 96.88% +0.02%
==========================================
Files 121 122 +1
Lines 1273 1284 +11
Branches 312 313 +1
==========================================
+ Hits 1233 1244 +11
Misses 34 34
Partials 6 6
|
import { usePreservedState } from '../usePreservedState'; | ||
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'; | ||
import { useCallback, useRef } from 'react'; | ||
|
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.
작성했다가 다시 생각해보려 지웠던 내용인데요! 😂
제가 생각했을때에는 ref는 Element의 ref로만 사용될것같은데, 외부로 반환해준 ref의 current에 접근하여 값을 할당해주는 케이스가 존재할까요 ?? 👀
내부적으로는 ref에 current가 없을경우에 window를 주입해주어 사용하기에 실제 타입은 MutableRefObject
가 맞으나, 외부에 반환해주는 타입으로서의 ref는 MutableRefObject
보다는 RefObject
로 안내하여 current에 할당이 불가하도록 readonly하게 제공하면 어떨까 의견드려봅니다! 😃
(실제로는 현재 구조에서도 HTMLDivElement로 인해 할당 불가능한 것으로 나오긴 하나 의미상 변경 불가한 타입으로 안내하는게 자연스러워보이네요!)
Before
After
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.
@Sangminnn 좋습니다! 좋은 의견 감사합니다 🤗🤗🤗🤗🤗
@Sangminnn 의견주신 부분 수정 반영하였습니다 🤗 |
```ts title="typescript" | ||
// 함수 오버로딩 | ||
export function useScrollTo(autoScrollOptions?: ScrollToOptions): { | ||
ref: React.MutableRefObject<Window | null>; |
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.
이 부분도 수정되어야할것같네요!
추가적으로 RefObject를 사용하면 내부적으로 제네릭을 받고 null을 같이 가지고있어서 위 아래 모두 null은 포함하지 않아도 될것같습니다! 😄
interface RefObject<T> {
readonly current: T | null;
}
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.
@Sangminnn 헛 문서도 다시 수정하도록 하겠습니다!!
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.
@Sangminnn MutableRefObject는 null이 포함되어 있지 않아 마운트 이전에는 null을 갖기 때문에 명확한 타입을 위해 Window | null을 유지하도록 하겠습니다!
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.
긍정적으로 의견 수용해주셔서 감사합니다!
다른 부분은 전부 좋습니다! 너무 고생 많으셨습니다! 👍👍👍
Overview
issue: #286
마운트 시에 window 한정으로 scrollTo를 호출하던 기존
useWindowScrollTo
를 제거하고, 조금 더 범용적으로 사용 할 수 있는useScrollTo
신규 훅을 작성합니다.useScrollTo 훅이 반환하는
ref
를 특정 엘리먼트에 할당 후 함께 반환하는scrollTo
를 통해 해당 엘리먼트를 대상으로scrollTo
를 호출하는 커스텀 훅입니다.ref
를 할당하지 않으면 기본적으로window
를 대상으로 동작합니다.autoScrollOptions
인자를 할당하면 컴포넌트가 마운트 시 자동으로 타겟 엘리먼트를 대상으로scrollTo
가 호출됩니다.PR Checklist
Contributing Guide