Created
August 31, 2020 21:56
-
-
Save refayathaque/3d5c2e8c68698a9b5b8d12357d254cce to your computer and use it in GitHub Desktop.
Posts.js
This file contains hidden or 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, 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