Setup is es6 class:
export default class Gallery extends Component {
addPhotos() {
let data = [{}, {}, {}, {}, {}, {}]; // array of objects
let dataChunks = chunk(data, 2); // returns array of two arrays [[{}, {}, {}],[{}, {}, {}]]
// now here is where I am stuck. I want to:
return dataChunk[0].map((photo) => (
<PhotoComponent key={photo._id} photo={photo} />
));
// wait 500ms and then add 3 more photos to the gallery (essentially throttling the loading)
return dataChunk[1].map((photo) => (
<PhotoComponent key={photo._id} photo={photo} />
));
Ideally, I am able to load 3 photos and then add the next 3 photos after 500ms.
As a bonus, having a loading component in between...
Thoughts?
What I have tried:
1) Using state to store the array. The problem seems to be rendering the array and changing it can cause a stack overflow problem.
2) Using a for loop with setTimeout... for some reason, the component props become undefined.
setTimeoutthat changes the state withthis.setStateand you have some flag in the state that helps you determine what to render.statewould havenumberOfPhotosproperty, with3set by default, andthis.setState({ numberOfPhotos: 6 });done on timer.rendermethod would take thethis.state.numberOfPhotosvalue and render the corresponding number of photos. And you don't need chunks at all.