DEV Community

Omoju117
Omoju117

Posted on

【Please check my code】React bad pattern

I wrote createIdentifier for solving React famous bad pattern ‘Using indexes as a key’.
But I’m not sure is it OK.
If you would like, please check my code and give me some comments. Or give me example code that you think the best way.


const createIdentifier = (index: number):string => {
  return index.toString() + Math.random().toString();
}

return(
  <div>
    <ul>
      {items.map((item, index) => (
        <li key={createIdentifier(index)}>
          {item}
        </li>
      ))}
    </ul>
  </div>
)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)