AllPosts.jsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import React, { useEffect, useState } from 'react';
  2. import { connect } from 'react-redux';
  3. import { actionAllPosts, actionRemovePostsFeedAC } from '../actions';
  4. import { CPosts } from '../components/main/Posts';
  5. import { Container } from './Content';
  6. import { CPreloader } from './Preloader';
  7. const AllPosts = ({ onAllPosts, postsRemove }) => {
  8. const [checkScroll, setCheckScroll] = useState(true)
  9. useEffect(() => {
  10. if (checkScroll) {
  11. onAllPosts()
  12. setCheckScroll(false)
  13. }
  14. }, [checkScroll])
  15. useEffect(() => {
  16. document.addEventListener('scroll', scrollHandler)
  17. return () => {
  18. document.removeEventListener('scroll', scrollHandler)
  19. postsRemove()
  20. }
  21. }, [])
  22. const scrollHandler = (e) => {
  23. if (e.target.documentElement.scrollHeight - (e.target.documentElement.scrollTop + window.innerHeight) < 300) {
  24. setCheckScroll(true)
  25. }
  26. }
  27. return (
  28. <Container>
  29. <CPreloader promiseName='allPosts' />
  30. <CPosts />
  31. </Container>
  32. )
  33. }
  34. export const CAllPosts = connect(null, { onAllPosts: actionAllPosts, postsRemove: actionRemovePostsFeedAC, })(AllPosts)