ReactJS - Keys

Keys avail React identify which items have transmuted, are integrated, or are abstracted. Keys should be given to the elements inside the array to give the elements a stable identity.

Example:

 


import React from 'react';

class App extends React.Component {
constructor() {
super();

this.state = {
data: [
{
language: 'Java....',
id: 1
},
{
language: 'C....',
id: 2
},
{
language: 'C++....',
id: 3
},
{
language: 'GO....',
id: 4
}
]
}
}
render() {
return (
<div class="container">
<div>
{this.state.data.map((dynamicComponent, i) =>
<Content key={i} componentData={dynamicComponent} />)}
</div>
</div>
);
}
}
class Content extends React.Component {
render() {
return (
<div>
<div>{this.props.componentData.language}</div>
<div>{this.props.componentData.id}</div>
</div>
);
}
}
export default App;

More...

Comments