在Redux中组成高阶减速器 [英] Composing higher order reducers in Redux

查看:87
本文介绍了在Redux中组成高阶减速器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些工厂函数,这些函数为我提供了简单(或更高级)的reducer.例如(简单的一种-基于操作类型,将RequestState常量设置为值):

I've created some factory functions that give me simple (or more advanced) reducers. For example (simple one - base on action type set RequestState constant as a value):

export const reduceRequestState = (requestTypes: RequestActionTypes) =>
    (state: RequestState = RequestState.None, action: Action): RequestState => {
        switch (action.type) {
            case requestTypes.start:
                return RequestState.Waiting;
            case requestTypes.success:
                return RequestState.Success;
            case requestTypes.error:
                return RequestState.Error;
            case requestTypes.reset:
                return RequestState.None;
            default:
                return state;
        }
    };

使用这些工厂功能和redux中的combineReducers,我可以将它们组合成功能齐全的reducer,以处理我的大多数临时动作.这使我的代码可读性强,并防止我犯一些愚蠢的错误.

Using those factory functions and combineReducers from redux I can compose them into fully functional reducer that handles most of my casual actions. That gives me readable code and prevents me from making silly mistakes.

工厂适用于常见操作,但是当我需要添加一些自定义行为(针对操作类型)时,应该对存储的某些部分进行重大修改,我想编写化简版的自定义部分来为我处理该操作. 这个想法是用迭代的方式组成化简器,所以combineReducers只是一个数组.这样,我可以使用工厂创建的reducer,然后将其与处理某些特定操作的自定义reducer结合起来.然后,数组的combineReducers会调用第一个数组,识别出什么都没有改变,然后调用第二个(自定义)数组来处理操作.

Factories are good for common actions but when I need to add some custom behavior (for action type) which should modify some part of the store significantly I would like to write a custom part of the reducer that will handle that action for me. The idea is to compose reducers in an iterate manner, so combineReducers but for an array. This way I could use my factories creating reducer and then combine it with my custom reducer that handles some specific actions. The combineReducers for an array would then call the first one, recognize that nothing has changed and call the second (custom) one to handle the action.

我一直在寻找解决方案并找到了redux-actions,但是我不太喜欢它将动作和化简链接起来的方式,这使得语义与我以前所用的语言几乎没有什么不同.也许我不明白,但是最终我希望看到我的reducer是作为纯函数编写的.

I was looking for some solution and found redux-actions but do not quite like the way it links actions and reducers making the semantics little different from what I'm used to. Maybe I do not get it, but eventually I like to see that my reducer is written as pure function.

我正在寻找一些提示,可以告诉我方法. 是否有使用任何一种高阶缩减器并以某种方式组合它们的库或项目? 如上所述组成减速器是否有不利之处?

I am looking for some hint that will show me the way. Is there any library or project that uses any kind of higher order reducers and combines them in some way? Are there any downsides regarding composing reducers like described above?

推荐答案

是的,由于化简器是 just 函数,因此可以使用无数种方法来组织逻辑,并且将多个函数组合在一起是非常鼓励.

Yep, since reducers are just functions, there's an infinite number of ways you can organize the logic, and composing multiple functions together is very encouraged.

您正在寻找的数组中的约简"想法是 https://github.com/acdlite/reduce-reducers .我经常在自己的应用中经常使用它来实现这种行为-首先运行combineReducers生成的化简器,然后依次运行化简器以实现更特定的行为.

The "reducers in an array" idea you're looking for is https://github.com/acdlite/reduce-reducers. I use it frequently in my own app for exactly that kind of behavior - running a combineReducers-generated reducer first, then running reducers for more specific behavior in turn.

我已经为Redux文档编写了一个名为构造Reducer 的部分,其中涵盖了与减速器逻辑相关的许多主题.这包括除常规combineReducers方法之外的有用模式.

I've written a section for the Redux docs called Structuring Reducers, which covers a number of topics related to reducer logic. That includes useful patterns beyond the usual combineReducers approach.

我还有一个许多其他与reducer相关的实用程序的列表作为我的 Redux插件目录的一部分.

I also have a list of many other reducer-related utilities as part of my Redux addons catalog.

这篇关于在Redux中组成高阶减速器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆