feat(useInfiniteScroll): using useInfiniteScroll with isInverse prop #2315
+64
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[中文版模板 / Chinese template]
🤔 This is a ...
🔗 Related issue link
First and foremost, I want to express my deep appreciation for the ahooks project. It has been an invaluable addition to the React ecosystem, providing a collection of useful hooks that have played a significant role in helping the React community thrive.
💡 Background and solution
Problem and Scenario:
I've submitted this pull request to tackle a common challenge when implementing chat-like interfaces, where users often need to scroll upwards to load more messages or content. The problem arises because there is currently no built-in functionality to change the scroll direction and trigger the loading function.
Currently, without any props to modify the scroll direction, users must rotate the list and apply CSS tricks to simulate an upward scroll. This workaround is not intuitive and can lead to suboptimal user experiences.
Solution:
To improve this situation, I would suggest adding the capability to trigger the loading function when scrolling up, which aligns with common chat application behavior. I've implemented a straightforward logic to achieve this:
When a user scrolls up, the scrollTop value becomes negative relative to the container we monitor for scroll events.
If the sum of the negative scrollTop and the scroll height is less than or equal to the client height, and the chosen threshold is met, we can conclude that the user has scrolled to the top of the page.
At this point, we can trigger the load function, ensuring a smooth and familiar experience for users.
Final API Implementation:
As part of this pull request, I've introduced a new prop called isInverse to the useInfiniteScroll hook. When set to true, this prop specifies that the desired scroll direction should be upward. This allows developers to easily configure the scroll behavior to align with common chat application requirements, where users scroll up to load more messages or content.
📝 Changelog
☑️ Self Check before Merge