我正在使用InfiniteScroll(react-infinite-scroll-component)。这是相反的顺序。但每当我使用API加载新数据时,它都会导致页面底部。下面是我的代码:
<div
id="scrollableDiv"
style={{
flex: 1,
overflow: "auto",
display: "flex",
flexDirection: "column-reverse",
}}
>
<InfiniteScroll
dataLength={messageGroups.length}
onScroll={e => {
e.preventDefault();
const scrollHeight = e.target.scrollHeight;
const varHeight = e.target.scrollTop - e.target.clientHeight;
console.log("varHeight", varHeight);
if (scrollHeight + varHeight < 1) {
onFetchPreviousMessages();
}
}}
style={{ display: "flex", flexDirection: "column-reverse" }} //To put endMessage and loader to the top.
id="scrollableDiv"
ref={scrollableDiv}
scrollableTarget="scrollableDiv"
/>
<div ref={messageRef} />
{!messageGroups.length && composeType === "help" && (
<ExplorerLargeInfoBlock
icon={<FaLifeRing />}
header="Help Request"
message="Need help? Send us a message below and someone on the project team will get back to you shortly."
/>
)}
{messageGroups.map(renderMessageGroup)}
</div>
I referred some posts where the div was being used, but not sure how to access the div.current in my case.
onFetchPreviousMessages is simply calling data API to fetch new data
字符串
1条答案
按热度按时间sauutmhj1#
要访问您的例子中的div.current,可以使用
useRef
钩子创建对scrollableDiv的引用。然后,您可以访问当前属性以获取底层DOM元素并操纵其滚动行为。个字符