Skip to content

Instantly share code, notes, and snippets.

@react-ram
Created October 23, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save react-ram/ac07b81fb54394cb05b44f42a0dd0f56 to your computer and use it in GitHub Desktop.
Save react-ram/ac07b81fb54394cb05b44f42a0dd0f56 to your computer and use it in GitHub Desktop.
Basics - Lists & keys example 4. usage of same key for different arrays.
import React, { Component } from "react";
export class App extends Component {
render() {
const posts = [
{ id: 1, title: "hello world", content: "welcome to the react world" },
{
id: 2,
title: "installation",
content: "you can install react from npm"
}
];
return (
<div>
<Blog posts={posts} />
</div>
);
}
}
function Blog(props) {
const posts = props.posts;
const sidebar = (
<ul>
{posts.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
const content = posts.map(post => (
<div key={post.id}>
<h3>{post.title}</h3>
<p>{post.content}</p>
</div>
));
return (
<div>
{sidebar}
<hr />
{content}
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment