index.js 819 B

1234567891011121314151617181920212223242526
  1. import { useEffect } from "react";
  2. import { useDispatch } from "react-redux";
  3. import { actionFeedAdd } from "../../../reducers";
  4. export const InfScroll = ({ component = null, onScroll = null, items = [], promiseStatus, ...restProps } = {}) => {
  5. const C = component;
  6. const dispatch = useDispatch();
  7. useEffect(() => {
  8. window.onscroll = (e) => {
  9. if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 400) {
  10. if (promiseStatus !== "PENDING") {
  11. onScroll && onScroll();
  12. }
  13. }
  14. };
  15. return () => {
  16. window.onscroll = null;
  17. };
  18. }, [onScroll]);
  19. useEffect(() => {
  20. if (items?.length) dispatch(actionFeedAdd(items));
  21. }, [items]);
  22. return <C {...restProps} />;
  23. };