Skip to content

Instantly share code, notes, and snippets.

@patwadeepak
Created July 23, 2021 07:04
Show Gist options
  • Save patwadeepak/0dd9e2335da70705b08748b53eb25f52 to your computer and use it in GitHub Desktop.
Save patwadeepak/0dd9e2335da70705b08748b53eb25f52 to your computer and use it in GitHub Desktop.
The Redux Way - Post.js
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