학습목표

Infinite Scroll이 뭘까

2021-09-23 11.17.48.gif

Intersection Observer를 이용한 일반적인 방법

Untitled

import React, { useEffect, useRef, useState } from 'react'
import axios from '../utils/fakeAxios';

const InfiniteScroll = props => {
    const ref = useRef(null);
    const [data, setData] = useState([]);

    const callApi = async () => {
        const listData = await axios.get();
        setData(listData);
    }

    const observer = new IntersectionObserver((entries, observer) => {
        entries.forEach(entry => {
            if(entry.isIntersecting){
                callApi();
            }
        })
    })

    useEffect(() => {
        callApi(); 
        observer.observe(ref.current);
    }, [])

    return (
        <div className="infiniteScroll">
            {data.map(({id, name}) => (
                <div className="infiniteScroll-item" key={id}>
                    <span>{name}</span>
                </div>
            ))}
            <div ref={ref}></div>
        </div>
    )
}

export default InfiniteScroll

위 방식의 문제점

Untitled

Virtual Scroll을 이용하자

Virtual Scroll이 뭘까