1

So I'm learning react and js, and I'm trying to loop every 20 (the limit in a single page) and show this pictures, also show in the bottom this pages index using bootstrap. but it not really working this is my code:

const pictureItems = this.state.imgFiles.map((img, index) => {
  return (
    <PictureListItem
      id={index}
      key={`img-${img.name}`}
      imgFile={img}
      pictureDataUpdate={this.onUpdatePicture}
    />
  );
});
const pageNumber = this.state.imgFiles.length / 20;
let pages = "";
for (let i = 0; i < pageNumber; i++) {
  pages += <li><a>{i}</a></li>;
  return pages;
}

I was thinking may be I can pass the value of the index to the loop and multiply by 20 for the start and then add 20 to the end. but I can't even get the pages to show well.

thoer metn
  • 61
  • 2
  • 1
    Possible duplicate of [Split array into chunks](http://stackoverflow.com/questions/8495687/split-array-into-chunks) – Jared Smith Oct 21 '16 at 16:59

1 Answers1

0

protip: don't do yourself what the language already does on its own.

const picturesPerPage = 20;
const images = this.state.imgFiles;

...

// get the current page, rounded down. We don't want fractions
let currentPageNumber = (images.length / picturesPerPage)|0;

// find the start and end position in the "images" array for this page
let start = currentPageNumber * picturesPerPage;
let end = (1+currentPageNumber) * picturesPerPage;

// cool: make JS get those items for us, and then map those items to bits of JSX
let pages = images.slice(start, end).map(img => {
  return (
    <li key={img.src}>
      <a href={img.href}>
        <img src={img.src} alt={img.alt}/>
      </a>
    </li>
  );
});

// and we're done.
return <ul>{ pages }</ul>;

Note that if you're building an array of on-the-fly React elements, they need to have a key attribute so that the React diff engine can properly do its job - the key needs to uniquely identify the actual thing, so you can't use array position (['a','b'] and ['b','a'] are the same array, but if you pretend the array position is a key, rather than "just swap the two elements" you're lying about what happens going from one to the other, claiming actual content changes occurred when they didn't, and things get horribly inefficient).

Also note that you tried to use += for adding elements into an array - that is illegal syntax, += is string concatenation. To add individual elements to an array you use array.push (or if you need to so something weird, array.splice)

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • You have way too much rep to be answering an obvious dupe. – Jared Smith Oct 21 '16 at 17:00
  • 2
    and on the contrary: I have enough rep to know I can answer this question in as long as it takes to write down the text, be no skin off my back, and help someone who obviously right now needs that help. Telling them "this has been asked before" is a great way to effectively shoot down someone's honest attempt at learning a new thing. They took the effort to ask a good question, with code, which is extremely rare for first time posters, and so that effort deserves a response in kind. Ask a good question, get an answer. Ask a bad one and get a "close as duplicate". – Mike 'Pomax' Kamermans Oct 21 '16 at 17:08
  • You have enough rep to know that getting closed as a duplicate *is not a bad thing*: ["Duplicate questions are not necessarily bad; different descriptions of the same problem help future visitors to find the answers they're looking for."](http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled). They will get their answer and learn the "new thing". True, it means fewer opportunities for answerers to scrounge up more rep by answering already-answered questions, but in the long run it's better for visitors. – Heretic Monkey Oct 21 '16 at 18:41