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

在单个组件中使用多个redux connect hoc

  •  1
  • Michal  · 技术社区  · 6 年前

    嗨,最近我看到一段有趣的代码。

    有一个特别的:

    import { connect } from 'react-redux'
    
    const mapProps = store => ({
      someProp: store.some.prop,
    })
    
    const withSomeProp = connect(mapProps)
    
    export default withSomeProp
    

    然后它被这样使用:

    export default compose(
      withSomeProp,
      connect(
        mapProps,
        mapDispatch
      ),
    )
    

    这种方法好吗?或者我应该担心潜在的性能问题或任何其他错误吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   markerikson    6 年前

    我通常建议定义一个更复杂的 mapState 函数,该函数提取此组件所需的两部分数据,而不是定义多个 connect 定义只是为了提取不同的数据片段。