【问题标题】:Should action creator trigger another action?动作创建者是否应该触发另一个动作?
【发布时间】:2015-10-16 04:52:10
【问题描述】:

例如,我将实现“点赞帖子”功能。帖子被点赞后,我需要获取最新的点赞数(另一个操作)。

从下面的代码中,我应该立即调用 get(postId) 来触发另一个动作,还是应该在处理“LIKE_POST_SUCCESS”时存储触发器。商店可以触发动作吗?

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
            });
        });
    }
}

【问题讨论】:

    标签: reactjs-flux flux


    【解决方案1】:

    商店不应该触发动作,而动作可能会触发其他动作。所以你的代码对我来说看起来不错。在您处理LIKE_POST_SUCCESS 的商店中,您可以在发出更改之前waitFor GET_POST_SUCCESS

    【讨论】:

    • 谢谢。你提醒我,我可以在 GET_POST_SUCCESS 之后发出更改。
    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2020-10-26
    相关资源
    最近更新 更多