【问题标题】:React/Redux, chain or promise in reducerReact/Redux,reducer 中的链或承诺
【发布时间】:2015-12-29 03:48:22
【问题描述】:

我有一个 react/redux 应用程序,我在其中进行了一些过滤和搜索。由于 API 的设置方式,我必须在接收过滤器和发送新的搜索查询之间做一些基础工作。所以我有一个活动过滤器的小参考图,每次我选中或取消选中一个过滤器时都会更新。问题是,我希望运行更新过滤器的操作,然后使用新过滤器参数调用服务器的操作,我不确定这个工作流应该如何进入 redux。

所以我调用了这个动作,然后它以这样的更新状态命中减速器 -

 case actions.TOGGLE_FILTER:
            return toggleFilter(state, action);


var toggleFilter = function(state, action){
var currentFilters = state.toJS().activeFilters || {};
var removeFilterFromActive = function(item) {
    if(item != action.filter.search){
        return;
    }
}
//already exists, remove from list (toggle off)
if (currentFilters[action.filterGroup.parentSearch] && currentFilters[action.filterGroup.parentSearch].indexOf(action.filter.search) > -1) {
    var itemIndex = currentFilters[action.filterGroup.parentSearch].indexOf(action.filter.search);
    currentFilters[action.filterGroup.parentSearch].splice(itemIndex, 1);
} else {
    //add to obj
    var newObj = {};
    if (currentFilters[action.filterGroup.parentSearch]) {
        currentFilters[action.filterGroup.parentSearch].push(action.filter.search);
    } else {
        newObj[action.filterGroup.parentSearch] = [];
        newObj[action.filterGroup.parentSearch].push(action.filter.search);
        _.extend(currentFilters, newObj);
    }
}
return state.set('activeFilters', fromJS(currentFilters));
};

所以这组装了我的activeFilters 状态,现在似乎工作正常。但我不知道的部分是如何使用我更新的 activeFilters 调用服务器。现在我只是从它正在使用的组件中调用这个动作。

当这个动作完成时,有没有办法在reducer 中链接、承诺或分派另一个动作?任何有关如何解决此问题的建议将不胜感激。谢谢!

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    reducer 应该是纯的,没有副作用,所以你不希望你的 reducer 向服务器发送请求或发出额外的操作。

    如果您使用redux-thunk,那么您可以将函数作为操作分派。这些函数可以检查商店的状态。这些函数本身可以调度多个常规动作。而且,如果您没有对 Redux 更新进行任何形式的批处理,他们可以在调度操作后检查存储,然后执行更多操作。

    考虑到上述情况,您可以执行以下操作:

    function createToggleFilterAndQueryServerAction(filterGroup, filter) {
        return (dispatch, getState) => {
            // first dispatch the filter toggle
            // if you do not have any batching middleware, this
            // should run the reducers and update the store
            // synchronously
            dispatch(createToggleFilter(filterGroup, filter));
    
            // Now get the updated filter from the store
            const activeFilter = getState().activeFilter;
    
            // now query the server
            callServer(activeFilter).then(result => {
                // Now dispatch an action with the result from the server
                dispatch(createServerResponseAction(result));
            });
        };
    }
    

    用法:

    dispatch(createToggleFilterAndQueryServerAction(..., ...));
    

    【讨论】:

    • 对我来说,服务器调用都是在中间件中设置的——它检查每个动作的特定值并拦截。所以对于你那里的查询部分,我可以分派第二个动作吗?谢谢!
    • 当然。只需使用更新的过滤器调度操作
    • 感谢您回答这个问题,这对我来说仍然是新的,有点不知所措。最后一个问题 - 我的状态正在使用 immutablejs,我注意到您正在传递 getState,这仍然适用于 immutable 吗?
    • getState 会给你redux store的内容。因此,如果您将不可变对象放入存储中,那么getState() 将返回那些不可变对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2017-03-17
    • 2018-09-19
    • 2021-08-10
    相关资源
    最近更新 更多