chachathok
chachathok

Reputation: 83

How do I use the compose function (of redux/ramda/recompose/etc)?

I get that something like foo(bar(baz))) can be re-written as compose(foo, bar, baz). What about real-life examples, however? For instance, I may have:

export default {
loadData,
component: connect(mapStateToProps, {
    actionCreator1,
    actionCreator2
})(requireAuth(showToggle({ChildComponent: aComponentOfMine, anotherField: `becauseStringsAreCool`})))

Is it possible to refactor the component value using compose?

Upvotes: 0

Views: 424

Answers (1)

Scott Christopher
Scott Christopher

Reputation: 6516

The component value in your example could be rewritten to use compose like so:

const buildComponent = compose(
  connect(mapStateToProps, {
    actionCreator1,
    actionCreator2
  }),
  requireAuth,
  showToggle
)

export default {
  loadData,
  component: buildComponent({
    ChildComponent: aComponentOfMine,
    anotherField: `becauseStringsAreCool`
  })
}

Upvotes: 2

Related Questions