M
M
mrrangerr2020-04-14 13:14:18
JavaScript
mrrangerr, 2020-04-14 13:14:18

How to prevent re-rendering when new data is loaded?

Hello everyone, I'm making a photo application where on the main page I load an endless list of photos using the ReactInfiniteScroll library. Every time I reach the end of the page, a new list of photos is loaded and the page is re-rendered. How to prevent it?

function Test() {
    const [photos, setPhotos] = useState([])
    const [perPage, setPerpage] = useState(1)

    const getData = useCallback(() => {
        unsplash.photos
            .listPhotos(perPage, 15, "latest")
            .then(toJson)
            .then(json => {
                setPhotos([...photos, ...json])
                setPerpage(perPage + 1)
            })
    }, [photos, perPage])

    useEffect(() => {
        getData()
    }, [])

   
    const styles = {
        padding: "15px"
    }

    const childElements = photos.map(photo => (
        <li className="list-unstyled" key={uuidv4()}>
            <Link to={`/${photo.id}`} key={uuidv4()} >
                <img className="rounded" style={styles} src={photo.urls.small} alt={photo.description}></img>
            </Link>
        </li>
    ))

    return (
        <div>
            <InfiniteScroll dataLength={photos.length}
                next={getData}
                hasMore={true}
                loader={<img src="loader.png" alt="loader"></img>}
            >
                <Masonry elementType={"ul"}>
                    {childElements}
                </Masonry>
            </InfiniteScroll>
        </div>
    )
}

export default Test

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2020-04-14
@mrrangerr

you create new Identifiers for the key attribute every time, but you need to use end-to-end through all requests

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question