Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 알고리즘
- 함수선언
- 분할정복
- 자료구조
- 얕은비교
- 백준
- hash table
- 해시함수
- 옵셔널체이닝
- 균형이진트리
- 리터럴
- 함수표현
- 원시값
- heapify
- 힙정의
- 리액트
- 호이스팅
- 삽입
- quadratic probing
- 힙트리
- 자바스크립트
- literal
- 해시테이블
- 참조타입
- 리렌더링
- null병합 연산자
- 참조값
- 상수시간
- 원시타입
- 힙성질
Archives
- Today
- Total
천천히 하지만 더 멀리
RxJs 를 이용한 스크롤 이벤트 스트림화 본문
많은 프론트앤드 개발자들은 스크롤 이벤트를 활용하여 개발(대표적으로 infinite scroll)을 하는 경우가 많이 있을것입니다.
한번의 스크롤로도 많은 스크롤 이벤트가 불리기 때문에 throttle 기법으로 이벤트를 처리하곤 합니다.
throttle 기법을 rxjs 이용하여 스트림화된 이벤트를 처리하는 코드를 작성해보겠습니다.
throttle 이란?
이벤트가 처음 발생하고 throttle Time 으로 지정한 시간동안 발생하는 이벤트는 무시하는 기법
구현 코드
import { ReactNode, useEffect, useRef } from 'react'
import { fromEvent, throttle, interval, Subject } from 'rxjs'
const subject$ = new Subject<number>()
interface Props {
children: ReactNode
}
export default function ScrollView({children}: Props) {
const currentScrolledTop = useRef<number>(0)
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
subject$.next(e.currentTarget.scrollTop)
}
useEffect(() => {
store$.pipe(throttleTime(500)).subscribe((top: any) => {
currentScrolledTop.current = top
})
numbers.subscribe(() => console.log('Next: ', currentScrolledPage.current))
}, [])
return (
<section onScroll={handleScroll}>
{children}
</section>
)
}
참고
Comments