hooks icon indicating copy to clipboard operation
hooks copied to clipboard

[BUG] useInfiniteScroll的target为document,计算target的高度是有问题的

Open taoliujun opened this issue 2 years ago • 3 comments

~/packages/hooks/src/useInfiniteScroll/index.tsscrollMethod方法中引用了getClientHeight方法,该方法代码如下:

const getClientHeight = (el: Document | Element) => {
  return (
    (el as Element).clientHeight ||
    Math.max(document.documentElement.clientHeight, document.body.clientHeight)
  );
};

在如上代码中,在业务里body的高度自适应的场景中,返回的其实是Math.max中的document.body.clientHeight,实际在整个逻辑中,期待返回的应该是document.documentElement.clientHeight

taoliujun avatar Apr 20 '22 03:04 taoliujun

临时的愚蠢解决方法:

const target = useMemo(() => {
      // fix useInfiniteScroll bug https://github.com/alibaba/hooks/issues/1563
      const doc = document;
      Reflect.defineProperty(doc, 'scrollTop', {
        get() {
          return document.documentElement.scrollTop;
        },
      });
      Reflect.defineProperty(doc, 'scrollHeight', {
        get() {
          return document.documentElement.scrollHeight;
        },
      });
      Reflect.defineProperty(doc, 'clientHeight', {
        get() {
          return document.documentElement.clientHeight;
        },
      });

      return doc;
  }, []);

taoliujun avatar Apr 20 '22 06:04 taoliujun

useInfiniteScroll 的 target 应该是固定高度的。

在你的代码中,documentdocument.body 的高度都不是固定的,都是会被无限撑起来。

brickspert avatar Apr 23 '22 08:04 brickspert

useInfiniteScroll 的 target 应该是固定高度的。

在你的代码中,documentdocument.body 的高度都不是固定的,都是会被无限撑起来。

对,你的说明只对了一半的。因为document的可视区域应该取document.documentElement.clientHeight,而不是Math.max(document.documentElement.clientHeight, document.body.clientHeight)

taoliujun avatar Apr 25 '22 14:04 taoliujun

+1,这个问题为何那么久了还不解决

limi58 avatar Dec 09 '22 09:12 limi58

+1,这个问题为何那么久了还不解决

fixed, see: https://github.com/alibaba/hooks/pull/2119

liuyib avatar Sep 27 '23 06:09 liuyib