Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Infinite Scroll #75

Merged
merged 7 commits into from
Oct 15, 2020

Conversation

icegotcha
Copy link
Contributor

@icegotcha icegotcha commented Oct 15, 2020

Description

According to #60, I updated the viewer to fully support infinite scroll as follow:

  • Update function "fetchData" to fetch only 5 posts in one time & support fetching after a post ID (and remove posts which have no image).
  • Add a loading spinner that will display at the bottom when user scroll to bottom of the webpage. I used react-spinners to implement it.
  • Add useEffect() to 3 function
    • first useEffect() will work if a user loads the webpage and search with a new term.
    • second useEffect() will work after first useEffect() (when content is fully loaded). This will create an observer (called Intersection Observer API) that handles loading more content.
    • third useEffect() will work if the user scrolls to loading more areas (handled by the observer).
  • Update preload link in siteHead.js to fetch only 5 posts.

Besides, I also made minor changes:

  • Let the state results store an array instead. I found it's easier to handle displaying posts.
  • Ruduced calling setSearchTerm to solve performance issues.
  • Update shrinkHeader to check if el is null before adding a listener and return a removing listener function used in the first useEffect().

Screenshot

https://imgur.com/a/8rTfmKs

…move fetch data function to out of UseEffect- Add a loading more element
Add observer to handle load more content
Move logic from handleLoadingMore to useEffect
Improve shrink header function to return removing listener function used in unmount section
Display posts contain image only
Update loading more spinner
@vercel
Copy link

vercel bot commented Oct 15, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/gregrickaby/reddit-image-viewer/35w1orjq5
✅ Preview: https://reddit-image-viewer-git-feature-infinite-scroll.gregrickaby.vercel.app

Copy link
Owner

@gregrickaby gregrickaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@icegotcha Wow! This is perfect, thank you! Feel free to take on other issues

@gregrickaby gregrickaby merged commit a8c5af3 into gregrickaby:main Oct 15, 2020
@icegotcha icegotcha deleted the feature/infinite-scroll branch October 15, 2020 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants