I have a React component that is supposed to load 6 images at a time as the page is scrolled lower. I want the typical infinite scroll effect YouTube and Reddit now use.
Right now the page opens as it should with 6 images loaded. When I scroll down the app loads another 6 but then it stops. There are 25 images total. I count 13 images that are not loading for some reason. The app is supposed to keep loading images 6 at a time when scrolling down until there are no more images to load.
I am using the Infinite Scroll component here. I've tried installing an older version number '4.5.2' and that didn't work either.
A screen shot of the app at the bottom shows buttons at the top which add or remove 6 images.
The first number shows the increments of 6 images. (It's 0 when 6 images are rendered and 1 when 12 are rendered.)
The number below that shows the number of total images (image URLs in the JSON object the app has received from the back end).
import React, { ListView } from 'react';
import InfiniteScroll from 'react-infinite-scroll-component';
export default class Fb_grid extends React.Component {
constructor(props) {
super(props)
this.state = {
apiResponse: [],
page: 0
}
}
componentDidMount() {
this.callAPI();
}
callAPI() {
fetch('/api/getList')
.then(res => res.json())
.then(res => this.setState({ apiResponse: res }))
}
next = () => {
this.setState({ page: this.state.page+1 })
}
render() {
const { apiResponse } = this.state;
const { page } = this.state;
if(!apiResponse) {
return <p className="row">Loading</p>
}
return(
<div className="App">
<div className="row">
<button className="testButton" onClick={this.previous}>LESS</button>
<button className="testButton" onClick={this.next}>MORE</button>
<br/>{page}<br/>{apiResponse.length}<div className="spacer"/>
</div>
<div className="row">
<InfiniteScroll
dataLength={apiResponse.length}
next={this.next}
hasMore={true}
>
{Object.values(apiResponse.slice(0, (this.state.page+1)*6)).map((value, index) =>
<a target="_blank" rel="noopener noreferrer" href={value.post}>
<img src={value.image} className="image"/>
</a>)}
</InfiniteScroll>
</div>
</div>
)
}
}
If you would like to run this component on your machine you will probably want your own JSON stored locally in the component. Of course you will also have to adjust the Object.values(apiResponse.slice(0, (this.state.page+1)*6)).map line accordingly.
Here is what the JSON data my component is using looks like. 25 objects with an image URL and a link for the <a> tag the image is rendered in.
[{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"},
{"image":"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3.jpg","post":"http://www.google.com"}]
