Skip to content

Commit

Permalink
refactor: useLayoutEffect로 실행하던 스크롤 이동 로직 refCallback으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Creative-Lee committed Nov 5, 2023
1 parent 4bf1262 commit c7f3337
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions frontend/src/features/songs/hooks/useSongDetailEntries.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { useLayoutEffect, useRef } from 'react';
import { useCallback } from 'react';
import useFetch from '@/shared/hooks/useFetch';
import useValidParams from '@/shared/hooks/useValidParams';
import { getSongDetailEntries } from '../remotes/songs';
import type { Genre } from '../types/Song.type';

const useSongDetailEntries = () => {
const { id: songIdParams, genre: genreParams } = useValidParams();
const currentSongDetailItemRef = useRef<HTMLDivElement>(null);

const { data: songDetailEntries } = useFetch(() =>
getSongDetailEntries(Number(songIdParams), genreParams as Genre)
);

useLayoutEffect(() => {
currentSongDetailItemRef.current?.scrollIntoView({ behavior: 'instant', block: 'start' });
}, [songDetailEntries]);
const scrollIntoCurrentSong: React.RefCallback<HTMLDivElement> = useCallback((dom) => {
if (dom === null) return;

return { songDetailEntries, currentSongDetailItemRef };
dom.scrollIntoView({ behavior: 'instant', block: 'start' });
}, []);

return { songDetailEntries, scrollIntoCurrentSong };
};

export default useSongDetailEntries;
4 changes: 2 additions & 2 deletions frontend/src/pages/SongDetailListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SongDetailListPage = () => {
const { isOpen, closeModal } = useModal(true);
const [onboarding, setOnboarding] = useLocalStorage<boolean>('onboarding', true);

const { songDetailEntries, currentSongDetailItemRef } = useSongDetailEntries();
const { songDetailEntries, scrollIntoCurrentSong } = useSongDetailEntries();

const {
extraPrevSongDetails,
Expand Down Expand Up @@ -60,7 +60,7 @@ const SongDetailListPage = () => {
<SongDetailItem key={prevSongDetail.id} {...prevSongDetail} />
))}

<SongDetailItem ref={currentSongDetailItemRef} key={currentSong.id} {...currentSong} />
<SongDetailItem ref={scrollIntoCurrentSong} key={currentSong.id} {...currentSong} />

{nextSongs.map((nextSongDetail) => (
<SongDetailItem key={nextSongDetail.id} {...nextSongDetail} />
Expand Down

0 comments on commit c7f3337

Please sign in to comment.