compose(…functions)

Composes functions from right to left.

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.

Arguments

  • (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

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

  1. import { createStore, applyMiddleware, compose } from 'redux'
  2. import thunk from 'redux-thunk'
  3. import DevTools from './containers/DevTools'
  4. import reducer from '../reducers'
  5. const store = createStore(
  6. reducer,
  7. compose(
  8. applyMiddleware(thunk),
  9. DevTools.instrument()
  10. )
  11. )

Tips

  • 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!