Created
October 23, 2019 09:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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