Skip to content

Instantly share code, notes, and snippets.

@refayathaque
Created August 31, 2020 21:56
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 refayathaque/3d5c2e8c68698a9b5b8d12357d254cce to your computer and use it in GitHub Desktop.
Save refayathaque/3d5c2e8c68698a9b5b8d12357d254cce to your computer and use it in GitHub Desktop.
Posts.js
import React, { Fragment, useContext } from 'react'
import { PostsContext } from '../Hooks/PostsContext'
export default ({ listType='bodies', header='' }) => {
const [ state ] = useContext(PostsContext);
const renderFetchedData = () => {
if (state.isLoading) {
return <div>Loading...</div>
} else {
return (
<ul>
{state.data.map((post, index) => {
if (listType === 'title') {
return <li key={index}>{post.title}</li>
} else {
return <li key={index}>{post.body}</li>
}
})}
</ul>
)
}
}
return (
<Fragment>
<h3>Posts reusable component {header}</h3>
<h4>Post {listType} only</h4>
{state.request ? renderFetchedData() : <div>No data requested</div>}
</Fragment>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment