-
Notifications
You must be signed in to change notification settings - Fork 11
feat: 용어집 무한스크롤 #136
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: main
Are you sure you want to change the base?
feat: 용어집 무한스크롤 #136
Changes from 9 commits
3d402de
155eeb1
447aeea
7e0aac7
eaeae1a
c8c7df1
1efccad
a6fc028
da70de4
9ef2ba8
2ad8083
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,30 @@ | ||
| import React from 'react'; | ||
| import useIntersectionObserver from 'hooks/useIntersectionObserver'; | ||
| import React, { useEffect } from 'react'; | ||
|
|
||
| import { WikiWord } from '../../types'; | ||
| type wikiTableRowProps = { | ||
| description: string; | ||
| name: string; | ||
| last: boolean; | ||
| onNext: () => void; | ||
| }; | ||
| export default function WikiTableRow({ | ||
| name, | ||
| description, | ||
| last, | ||
| onNext, | ||
| }: wikiTableRowProps) { | ||
| const { ref, entry } = useIntersectionObserver({ freezeOnceVisible: true }); | ||
|
|
||
| export default function WikiTableRow({ name, description }: WikiWord) { | ||
| return ( | ||
| <tr> | ||
| useEffect(() => { | ||
| if (entry?.isIntersecting) onNext(); | ||
| }, [entry]); | ||
|
|
||
| const content = ( | ||
| <> | ||
| <td>{name}</td> | ||
| <td>{description}</td> | ||
| </tr> | ||
| </> | ||
| ); | ||
|
|
||
| return last ? <tr ref={ref}>{content}</tr> : <tr>{content}</tr>; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| import { useEffect, useState } from 'react'; | ||||||
|
|
||||||
| const usePagination = ({ source = [], limit = 10, offset = 0 }) => { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prop에 대해 interface를 정의하고 각 prop에 대한 설명이 있으면 좋을 것 같아요 :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분은 생각지 못했는데 감사합니다! 설명! |
||||||
| const getResult = () => { | ||||||
| const startIdx = 0; | ||||||
| const endIdx = currentOffest; | ||||||
| return source.slice(startIdx, endIdx); | ||||||
| }; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 타입 지정되면 좋을 것 같아요~
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 타입지정! |
||||||
|
|
||||||
| const getMaxOffset = () => { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return type도 명시되면 좋을 것 같습니다 ㅎㅎ
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 타입지정!!!@! |
||||||
| return source.length; | ||||||
| }; | ||||||
|
|
||||||
| const [currentOffest, setCurrentOffest] = useState(limit); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. state generic에는 type 지정을 해주는 게 좋아요 :) 지금은 limit 값으로 초기화를 해줘서 typescript가 setCurrentOffest() 함수를 썼을 때 number 타입 이외에 다른 값이 들어가지 않게 추론을 해주긴 하지만 IDE 환경에 의존해야 하는 부분이 있거든요. 초기화를 안했을 경우에는 어떤 타입으로 할당해야 하는지 추론이 안되구요 ㅎㅎ 그래서 일관성을 위해 다음처럼 작성하면 좋습니당!
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 타입지정을 자꾸 빼먹고 작성을하게 되네용! 꼭 기억하겠습니다! |
||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (currentOffest !== limit) setCurrentOffest(limit); | ||||||
| else getResult(); | ||||||
| }, [source]); | ||||||
|
|
||||||
| const onNext = () => { | ||||||
| if (currentOffest >= getMaxOffset()) return; | ||||||
| setCurrentOffest(currentOffest + limit); | ||||||
| }; | ||||||
|
|
||||||
| return { | ||||||
| result: getResult(), | ||||||
| currentOffest, | ||||||
| isLastOffset: currentOffest > getMaxOffset(), | ||||||
| onNext, | ||||||
| }; | ||||||
| }; | ||||||
|
|
||||||
| export default usePagination; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { RefObject, useEffect, useRef, useState } from 'react'; | ||
| import 'intersection-observer'; | ||
| interface Args extends IntersectionObserverInit { | ||
| freezeOnceVisible?: boolean; | ||
| delay?: number; | ||
| } | ||
|
|
||
| function useIntersectionObserver({ | ||
| threshold = 0, | ||
| root = null, | ||
| rootMargin = '0%', | ||
| freezeOnceVisible = false, | ||
| delay = 0, | ||
| }: Args): { | ||
| ref: RefObject<Element> | null; | ||
| entry: IntersectionObserverEntry | undefined; | ||
| } { | ||
| const ref = useRef<HTMLFormElement | null>(null); | ||
| const [entry, setEntry] = useState<IntersectionObserverEntry>(); | ||
|
|
||
| const frozen = entry?.isIntersecting && freezeOnceVisible; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. boolean type 지정이 있으면 좋을 것 같아요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 타입지정! |
||
| const updateEntry = ([entry]: IntersectionObserverEntry[]): void => { | ||
| setTimeout(() => { | ||
| setEntry(entry); | ||
| }, delay); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| const node = ref?.current; // DOM Ref | ||
| const hasIOSupport = !!window.IntersectionObserver; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 없을 것으로 보입니다! 제거하는게 좋아보입니다! |
||
| if (!hasIOSupport || frozen || !node) return; | ||
|
|
||
| const observerParams = { threshold, root, rootMargin }; | ||
| const observer = new IntersectionObserver(updateEntry, observerParams); | ||
|
|
||
| observer.observe(node); | ||
| return () => observer.disconnect(); | ||
|
|
||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [ref, threshold, root, rootMargin, frozen]); | ||
|
|
||
| return { ref, entry }; | ||
| } | ||
|
|
||
| export default useIntersectionObserver; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so 깔끔한 훅이네요! 굿굿 |
||
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.
layer tree가 업데이트 될 때마다 table의 width 값이 계속 바뀌네요 ㅎㅎㅠ 새로 추가되는 콘텐츠에 따라 기존에 있던 table의 너비가 달라지니 당연한 거긴 한데 사용자 경험 측면에서 자연스럽지 않아 보이는군요.. 반응형을 생각했을 때 table 구조가 용어 사전을 보기에 적절한 UI인지도 논의가 필요할 것 같아요
2021-10-08.8.45.00.mov
.
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.
아하 영상보고 이해가 되었습니다! 이부분은 적절한 UI로 변경되야될것으로 보이네요!