更正Flux Store中状态数组的正确方法

时间:2016-02-16 19:23:52

标签: reactjs flux

我有一个Flux Store和一个状态数组,需要在创建新注释时进行更新,以便在视图中更新注释列表。我只想确认我正在使用push正确更新它:

this.state.comments.push(commentArray);

它工作正常,但我已经阅读了不可变性但是因为这是一个商店,而不是一个视图,我认为这是好的吗?

 onDispatcherAction: function (payload) {

    var action = payload.action;
    if (ActionTypes.CREATE_CONFIGURATION_SETTINGS_RESPONSE === action.type) {
        this.handleResponseErrors(action.data);

        var commentArray = {
            _id: action.data._id,
            user: {
                id: action.data.user.id
            },
            id:action.data.id,
            commentname: action.data.commentname,
            timeCreated: action.data.timeCreated
        }
        this.state.commentname = action.data.commentname;

        this.state.comments.push(commentArray);

        this.emitChange();
    }

}

1 个答案:

答案 0 :(得分:1)

您可能应该查看Immutability Helpers

来自React Documentation

简单推送

array = Array(count: rows * columns, repeatedValue: nil)

var initialArray = [1, 2, 3]; var newArray = update(initialArray, {$push: [4]}); // => [1, 2, 3, 4]

相关问题