Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print numbers inside react component with map?

This is probably an easy one, but I can't seem to figure it out. How do I print the numbers in order inside my list? If I print index some of the numbers will be skipped due to my conditions? Can I set a count++ somewhere inside my map-function inside my condition so it will increase every time a list-item is printed?

export const MFAPaging = ({rootStore, page}) => {
  let count = 1;

  return (
    <div className="components-paging-brder">
      <div className="components-paging">
        <ul>
          {rootStore.mfaStore.links.map((item, index) =>
              item && item.includePage ? 
              <li className={page == index ? 'active' : ''} key={index}>{count}</li>
              : undefined 
            )}
        </ul>
        <MFAButtons rootStore={rootStore} page={page} />
    </div>
  </div>
  );
};
like image 644
Nirakander Avatar asked Mar 16 '26 03:03

Nirakander


1 Answers

First you can filter the array, and then your indexes will be cool in your .map (never use .map to filter, you already have build-in functions)

{rootStore.mfaStore.links.filter((item)=> item && item.includePage).map((item, index) =>
    <li className={page == index ? 'active' : ''} key={index}>{count}</li>
)}

For questions comment :)

like image 132
Renaldo Balaj Avatar answered Mar 17 '26 16:03

Renaldo Balaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!