将param传递给`connect()`中的mapStateToProps

use*_*079 4 reactjs react-native redux react-redux

如何将参数传递给mapStateToProps()

例如:

const mapStateToProps = (state, dimenType: string) => {

  var jsonVariable = {}
  for(var i=1; i <= 3; i++) {
    jsonVariable[dimenType + i] = dimenType + i
  }
  console.log(jsonVariable)
  return {
    width1: state.get('width').get('width1').toString(),
    width2: state.get('width').get('width2').toString(),
    width3: state.get('width').get('width3').toString()
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我不确定的一点:

Width = connect(
  mapStateToProps(/*redux store goes here somehow*/, 'thickness'),
  mapDispatchToProps
)(Width)
Run Code Online (Sandbox Code Playgroud)

我希望redux商店仍然可以传递,mapStateToProps但也通过"thickness".我该如何在connect()函数中执行此操作?

cub*_*buk 7

第二个参数connectownProps传递给它的道具Component.所以在你的情况下你可以像这样使用它:

const mapStateToProps = (state, ownProps) => {
  let jsonVariable = {}
  for(var i=1; i <= 3; i++) {
    jsonVariable[dimenType + i] = ownProps.dimenType + i
  }
  console.log(jsonVariable)
  return {
    width1: state.get('width').get('width1').toString(),
    width2: state.get('width').get('width2').toString(),
    width3: state.get('width').get('width3').toString()
  }
}
Run Code Online (Sandbox Code Playgroud)

我假设您正在创建这样的组件: <Width thickness={10}/>