Created
July 23, 2021 07:04
-
-
Save patwadeepak/0dd9e2335da70705b08748b53eb25f52 to your computer and use it in GitHub Desktop.
The Redux Way - Post.js
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 "../CSS/Post.css"; | |
import { useEffect } from "react"; | |
import { func, array, object } from "prop-types"; | |
import { connect } from "react-redux"; | |
import { fetchPosts } from "../actions/postActions"; | |
const Posts = ({ posts, fetchPosts }) => { | |
useEffect(() => { | |
fetch("https://jsonplaceholder.typicode.com/posts") | |
.then((res) => res.json()) | |
.then((posts) => { | |
fetchPosts(posts); | |
}); | |
}, []); | |
return ( | |
<div className="posts-container"> | |
<div> | |
<h1>{"Recent Posts"}</h1> | |
</div> | |
{posts.map((post, index) => ( | |
<div key={index}> | |
<div className="post-title">{post.title}</div> | |
<div className="post-body">{post.body}</div> | |
</div> | |
))} | |
</div> | |
); | |
}; | |
Posts.propTypes = { | |
posts: array.isRequired, | |
}; | |
const mapStateToProps = (state) => ({ | |
posts: state.posts.items, | |
}); | |
export default connect(mapStateToProps, { fetchPosts })(Posts); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment