Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define keys in React JSX when use Map function

I have Arrays of objects and needs to loop through it and display it in the DOM. I able to do it with map function. But i getting the error due to lack of providing unique keys for it. Remember, it is not using the component to loop. I just want JSX to loop through it. So how can we provide keys in it. The code is as follows. Thanks in Advance!

import React from 'react';
import {Component1, Component2} from './Component';

const data=[
  {
    title: "Sample Title One",
    description: "Sample Description One"
  },{
    title: "Sample Title Two",
    description: "Sample Description Two"
  }]

function App(){
  return(
    <div className="data">
      {data.map((item)=>{
        return <div className="wrapper">
          <div className="title">{item.title}</div>
          <div className="description">{item.description}</div>
        </div> 
      })}
    </div>
  )
}
export default App;

I tried the following
<div className="wrapper" keys={item.title}>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think it’s <div className="wrapper" key={item.title}>

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading