Skip to content

Instantly share code, notes, and snippets.

@tinykite
Created June 26, 2019 04:23
Show Gist options
  • Save tinykite/fc637b3a4106405011ae53504bc39cb7 to your computer and use it in GitHub Desktop.
Save tinykite/fc637b3a4106405011ae53504bc39cb7 to your computer and use it in GitHub Desktop.
Using React with Arrays and the Map function
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;
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;
[
{
"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