DEV Community

Discussion on: React infinite scroll in few lines

Collapse
 
raibtoffoletto profile image
Raí B. Toffoletto

Be carefull... that postList.list as dependencies will make your component enter an infinite loop of rerenders. It is better to access this property via an arrow function.

  useEffect(() => {
    setPostList((x) => ({
      list: x.concat([1, 1, 1, 1]),
    }))
  }, [page])
Enter fullscreen mode Exit fullscreen mode

Also, handleObserver is better to be insde the useEffect function and you should clear (observer.unobserve) the listener on a callback funtion. ;)