简体   繁体   English

在React中使用mapStateToProps和state与connect()

[英]Using mapStateToProps and state with connect() in React

I am using Redux-Form & am trying to get the values from that form. 我正在使用Redux-Form,并尝试从该表单获取值。 The documentation suggests that the way to use getFormValues() is as such: 文档建议使用getFormValues()方式如下:

MyComponent = connect(
  state => ({
    values: getFormValues('myForm')(state),
  })
)(MyComponent)

In my component, I am currently using connect with mapStateToProps in my component. 在我的组件中,我目前正在将connectmapStateToProps一起使用。 But how can I use both of these together? 但是,如何将两者同时使用? My current code (that doesn't work, as the personFormValues is undefined ), is as such: 我当前的代码(由于personFormValues是undefined ,因此不起作用)是这样的:

export default reduxForm({
  form: 'personsForm'
})(connect(mapStateToProps, state => ({
  personFormValues: getFormValues('personsForm')(state)
}))(PersonsForm));

I have also tried: 我也尝试过:

export default reduxForm({
  form: 'personsForm'
})(connect(state => ({
  personFormValues: getFormValues('personsForm')(state),
  mapStateToProps
}))(PersonsForm));

and

export default reduxForm({
  form: 'personsForm'
})(connect(state => ({
  personFormValues: getFormValues('personsForm')(state),
}), mapStateToProps)(PersonsForm));

and

export default reduxForm({
  form: 'personsForm'
})(connect(state => ({
  personFormValues: getFormValues('personsForm')(state),
  personsForm: state.form.personsForm
}))(PersonsForm));

In the latter instance, personsForm and personFormValues returns as undefined. 在后一种情况下,personForm和personFormValues返回未定义。

What is the correct way to use both with connect ? connectconnect一起使用的正确方法是什么?

I got it working. 我知道了 I just added it into the mapStateToProps method like so: 我只是将其添加到mapStateToProps方法中,如下所示:

const mapStateToProps = state => ({
  personsForm: state.form.personsForm,
  values: getFormValues('personsForm')(state)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM