在单个组件中使用多个Redux Connect HOC

时间:2018-11-29 14:16:38

标签: reactjs redux higher-order-functions higher-order-components

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

存在此HOC:

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 个答案:

答案 0 :(得分:0)

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