Franz Wong
Franz Wong

Reputation: 1114

Should action creator trigger another action?

For example, I am going to implement a "Like post" feature. After the post is liked, then I need to get the latest like count (another action).

From the following code, should I call get(postId) immediately to trigger another action or should store trigger that when handling "LIKE_POST_SUCCESS". Can a store trigger an action?

class PostActions {
    like(postId) {
        var self = this;
        PostApiUtils.like(postId).then(function() {
            AppDispatcher.dispatch({
                actionType: 'LIKE_POST_SUCCESS',
                postId: postId
            });
            self.get(postId);
        });
    }

    get(postId) {
        PostApiUtils.get(postId).then(function(post) {
            AppDispatcher.dispatch({
                actionType: 'GET_POST_SUCCESS',
                post: post
            });
        });
    }
}

Upvotes: 0

Views: 396

Answers (1)

gpbl
gpbl

Reputation: 4804

Stores should never trigger actions, while actions may trigger other actions. So your code looks fine to me. In your store, where you are handling LIKE_POST_SUCCESS, you could waitFor GET_POST_SUCCESS before emitting a change.

Upvotes: 1

Related Questions