Home > Software design >  How do you add a key to a variable map in javascript?
How do you add a key to a variable map in javascript?

Time:05-02

How would I add a key to a mapped variable statement like below?

  const limit = Math.max(...props.data.map(x => x.count), 0);

I'm getting an error in React stating "Each child in a list should have a unique "key" prop." I know how to add a key to an HTML object, but I'm not sure how to do it in this case.

CodePudding user response:

Basically this error means that you should add a 'key' prop for components when you do map() , to help react decide which components to re-render instead of re-rendering the whole list

you do for example :

myList.map( element  => <Component   key={element.id}     />  )

So the error is not related to the code you attached ,

CodePudding user response:

You can try doing something like this

let mp = new Map();
for(let i=0; i<data.length;i  ){
mp.set(MathmaxFunction,i)
}
console.log(mp.keys())
  • Related