Skip to main content

compose(...functions)

概述

¥Overview

从右到左组合函数。

¥Composes functions from right to left.

这是一个函数式编程实用程序,为了方便起见,它包含在 Redux 中。你可能想用它连续应用多个 存储增强剂compose 也可用作通用独立方法。

¥This is a functional programming utility, and is included in Redux as a convenience. You might want to use it to apply several store enhancers in a row. compose is also usable as a general-purpose standalone method.

警告

你不必直接致电 compose。Redux Toolkit 的 configureStore 方法 自动使用标准 applyMiddleware 和 Redux DevTools 存储增强器配置 Redux 存储,并提供 enhancers 参数来传递其他增强器。

¥You shouldn't have to call compose directly. Redux Toolkit's configureStore method automatically configures a Redux store with the standard applyMiddleware and Redux DevTools store enhancers, and offers an enhancers argument to pass in additional enhancers.

参数

¥Arguments

  1. (参数):要组成的函数。每个函数都应该接受一个参数。它的返回值将作为参数提供给左侧的函数,依此类推。例外的是最右边的参数,它可以接受多个参数,因为它将提供结果组合函数的签名。

    ¥(arguments): The functions to compose. Each function is expected to accept a single parameter. Its return value will be provided as an argument to the function standing to the left, and so on. The exception is the right-most argument which can accept multiple parameters, as it will provide the signature for the resulting composed function.

返回

¥Returns

(功能):将给定函数从右到左组合得到的最终函数。

¥(Function): The final function obtained by composing the given functions from right to left.

示例

¥Example

此示例演示如何使用 compose 通过 applyMiddlewareredux-devtools 包中的一些开发者工具来增强 store

¥This example demonstrates how to use compose to enhance a store with applyMiddleware and a few developer tools from the redux-devtools package.

import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import DevTools from './containers/DevTools'
import reducer from '../reducers'

const store = createStore(
reducer,
compose(applyMiddleware(thunk), DevTools.instrument())
)

提示

¥Tips

  • compose 所做的只是让你编写深度嵌套的函数转换,而不会导致代码向右漂移。不要给予它太多的信任!

    ¥All compose does is let you write deeply nested function transformations without the rightward drift of the code. Don't give it too much credit!