Created
June 26, 2019 04:23
-
-
Save tinykite/fc637b3a4106405011ae53504bc39cb7 to your computer and use it in GitHub Desktop.
Using React with Arrays and the Map function
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 from 'react'; | |
import posts from "./sampleContent.json"; | |
import Content from './Content'; | |
function App() { | |
return ( | |
<div className="App"> | |
<Content posts={posts} /> | |
</div> | |
); | |
} | |
export default App; |
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, {Fragment} from 'react'; | |
function Content(props) { | |
const content = props.posts.map((post) => | |
<div key={post.id}> | |
<h3>{post.title}</h3> | |
<p>{post.content}</p> | |
</div> | |
); | |
return ( | |
<Fragment> | |
{content} | |
</Fragment> | |
); | |
} | |
export default Content; |
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
[ | |
{ | |
"id": "1", | |
"title": "Hello World", | |
"content": "Welcome to learning React!" | |
}, | |
{ | |
"id": "2", | |
"title": "Installation", | |
"content": "You can install React from npm." | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment