[英]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. 在我的组件中,我目前正在将connect
与mapStateToProps
一起使用。 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
? connect
和connect
一起使用的正确方法是什么?
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.