Skip to content
This repository was archived by the owner on Jul 7, 2021. It is now read-only.

Commit 29df531

Browse files
committed
update Posts Component to render newly added post
1 parent faa789a commit 29df531

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/components/Posts.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class Posts extends Component {
88
this.props.fetchPosts();
99
}
1010

11+
componentWillReceiveProps(nextProps) {
12+
if (nextProps.newPost) {
13+
this.props.posts.unshift(nextProps.newPost);
14+
}
15+
}
16+
1117
render() {
1218
const postItems = this.props.posts.map((post) => (
1319
<div key={post.id}>
@@ -27,10 +33,12 @@ class Posts extends Component {
2733
Posts.protoType = {
2834
fetchPosts: PropTypes.func.isRequired,
2935
posts: PropTypes.array.isRequired,
36+
newPost: PropTypes.object,
3037
};
3138

3239
const mapStateToProps = (state) => ({
33-
posts: state.posts.items, // here we used key posts because we used the same key in rootReducer
40+
posts: state.posts.items,
41+
newPost: state.posts.item,
3442
});
3543

3644
export default connect(mapStateToProps, { fetchPosts })(Posts);

0 commit comments

Comments
 (0)