DEV Community

Discussion on: How to use loop in React.js?

Collapse
 
pengeszikra profile image
Peter Vivo • Edited

Maybe product is non unique, index still usefull

  const list = products.map((product, key) => <li key={key}>{product}</li>)
Enter fullscreen mode Exit fullscreen mode

... but better if prodcut contains key and info

  // products = [ {info: 'red apple', key: 'RB67'}, {info: 'green pie', key: 'CB732'}];
  const list = products.map(({info, key}) => <li key={key}>{info}</li>)
Enter fullscreen mode Exit fullscreen mode