如何使用compose函数(redux / ramda / recompose / etc)?

时间:2018-01-20 18:12:56

标签: reactjs functional-programming redux ramda.js recompose

我得到的东西就像foo(bar(baz)))可以重写为compose(foo,bar,baz)。然而,现实生活中的例子呢? 例如,我可能有:

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

是否可以使用compose重构组件值?

1 个答案:

答案 0 :(得分:2)

您的示例中的component值可以重写为使用compose,如下所示:

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

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