reactjs – 将param传递给`connect()`中的mapStateToProps
发布时间:2020-12-15 16:19:33 所属栏目:百科 来源:网络整理
导读:如何将参数传递给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: stat
如何将参数传递给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() } } 这是我不确定的一点: Width = connect( mapStateToProps(/*redux store goes here somehow*/,'thickness'),mapDispatchToProps )(Width) 我希望redux存储仍然传递到mapStateToProps,但也传递“厚度”.我如何在connect()函数中执行此操作? 解决方法
connect的第二个参数是ownProps,它是传递给Component的props.所以在你的情况下你可以像这样使用它:
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(),width3: state.get('width').get('width3').toString() } } 我假设您正在创建这样的组件:<宽度厚度= {10} /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |