Skip to content

Commit 17961a3

Browse files
committed
deconstrucing dispatch to props
1 parent 85b9bd2 commit 17961a3

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

src/components/Post.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React from "react";
22
import { connect } from "react-redux";
33
import { Link } from "react-router-dom";
4-
import { deletePost, castVote } from "./../actions/posts";
4+
import * as postsActions from "./../actions/posts";
55
import PostManager from "./PostManager";
66

77
const Post = props => {
88
const deletePost = () => {
99
let id = props.post.id;
10-
props.dispatchDeletePost(id);
10+
props.deletePost(id);
1111
};
1212
const votePost = payload => {
13-
props.dispatchCastVote(payload);
13+
props.castVote(payload);
1414
};
1515

1616
return (
@@ -38,12 +38,7 @@ const Post = props => {
3838
</li>
3939
);
4040
};
41-
const mapDispatchToProps = dispatch => {
42-
return {
43-
dispatchDeletePost: id => dispatch(deletePost(id)),
44-
dispatchCastVote: payload => dispatch(castVote(payload))
45-
};
46-
};
41+
4742
function mapStateToProps({ posts, loading }) {
4843
return {
4944
posts,
@@ -53,5 +48,5 @@ function mapStateToProps({ posts, loading }) {
5348

5449
export default connect(
5550
mapStateToProps,
56-
mapDispatchToProps
51+
postsActions
5752
)(Post);

src/components/PostDetail.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { Component } from "react";
22
import { connect } from "react-redux";
3-
import { getPostById } from "./../actions/post";
4-
import { castVote, deletePost } from "./../actions/posts";
5-
import { getCommentsByPostId } from "./../actions/comments";
3+
import * as postActions from "./../actions/post";
4+
import * as postsActions from "./../actions/posts";
5+
import * as commentsActions from "./../actions/comments";
66
import Comments from "./Comments";
77
import PostManager from "./PostManager";
88
import PostBody from "./PostBody";
@@ -11,19 +11,19 @@ import GenericNotFound from "./GenericNotFound";
1111
class PostDetail extends Component {
1212
componentDidMount() {
1313
let id = this.props.match.params.id;
14-
this.props.dispatchGetPostById(id);
15-
this.props.dispatchGetCommentsByPostId(id);
14+
this.props.getPostById(id);
15+
this.props.getCommentsByPostId(id);
1616
}
1717

1818
deletePost = () => {
1919
let id = this.props.match.params.id;
20-
this.props.dispatchDeletePost(id);
20+
this.props.deletePost(id);
2121
this.props.history.push("/");
2222
};
2323
votePost = payload => {
2424
let id = this.props.match.params.id;
25-
this.props.dispatchCastVote(payload);
26-
this.props.dispatchGetPostById(id);
25+
this.props.castVote(payload);
26+
this.props.getPostById(id);
2727
};
2828

2929
render() {
@@ -63,17 +63,7 @@ function mapStateToProps({ posts, post, loading, comments }) {
6363
};
6464
}
6565

66-
const mapDispatchToProps = dispatch => {
67-
return {
68-
dispatchGetPostById: id => {
69-
dispatch(getPostById(id));
70-
},
71-
dispatchGetCommentsByPostId: id => dispatch(getCommentsByPostId(id)),
72-
dispatchDeletePost: id => dispatch(deletePost(id)),
73-
dispatchCastVote: payload => dispatch(castVote(payload))
74-
};
75-
};
7666
export default connect(
7767
mapStateToProps,
78-
mapDispatchToProps
68+
{ postActions, postsActions, commentsActions }
7969
)(PostDetail);

0 commit comments

Comments
 (0)