代码之家  ›  专栏  ›  技术社区  ›  chachathok

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

  •  0
  • chachathok  · 技术社区  · 8 年前

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

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

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

    1 回复  |  直到 8 年前
        1
  •  2
  •   Scott Christopher    8 年前

    这个 component 您的示例中的值可以重写为使用 compose 像这样:

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