[英]redux-form: Accessing state and form-values on handleSubmit
I have the following redux-form (6.5.0) source code: 我有以下redux-form(6.5.0)源代码:
class MyForm extends Component { handleSubmit() { console.log("Inside the handleSubmit of the MyForm Component where state is accessible", this.state.MyStateElement); } function render() { return ( <<<< CHOICE 1: >>>> <form onSubmit={handleSubmit(this.handleSubmit.bind(this))} > <<<< CHOICE 2: >>>> <form onSubmit={handleSubmit(submit)}> {someFields} </form> ); } } function submit(values) { console.log("submit function outside the MyForm Component where form values are accessible but the State is NOT accessible", values); // values correspond to every checkbox, radio button inside MyForm }
There is a MyForm component. 有一个MyForm组件。 It has a redux-form inside.
它内部有一个redux形式。 Now there is a handleSubmit() function that is defined inside the react component.
现在,在react组件内部定义了一个handleSubmit()函数。 There is a submit() function that is defined outside the react component.
在react组件之外定义了一个commit()函数。 Now I can enable either the CHOICE 1 or the CHOICE 2 definition for the form tag.
现在,我可以为form标签启用CHOICE 1或CHOICE 2定义。 If I enable the first one, the handleSubmit inside the react component is called.
如果启用第一个,则会调用react组件内的handleSubmit。 Here, I am able to access the
this.state
but not able to access the individual fields inside the form (unless I could map each of the fields to a global state manually, which I could not do explicitly for the dozens of Fields that I have). 在这里,我可以访问
this.state
但不能访问表单中的各个字段(除非我可以手动将每个字段映射到全局状态,而我无法对我所创建的数十个字段进行明确的操作有)。 If I enable the CHOICE 2, the values for all the fields are so correctly coming to the submit
function. 如果启用了CHOICE 2,则所有字段的值都将正确
submit
到submit
函数。 But I am not able to access the this.state
inside the submit
function. 但我不能够访问
this.state
内submit
功能。
My question is: Is there a way to get the Field values as well as the this.state
in a handleSubmit/submit function ? 我的问题是:是否有一种方法可以在handleSubmit / submit函数中获取Field值以及
this.state
?
Okay, I found a solution (not sure if this is the best though). 好的,我找到了解决方案(虽然不确定这是否是最好的)。 I just had to add the values parameter to the
handleSubmit
function. 我只需要将values参数添加到
handleSubmit
函数。 So the updated source will be: 因此,更新后的源将是:
class MyForm extends Component { handleSubmit(values) { console.log("State:", this.state.MyStateElement); console.log("Form values:", values); } function render() { return ( <form onSubmit={handleSubmit(this.handleSubmit.bind(this))} > {someFields} </form> ); } }
Another solution would be... 另一个解决方案是...
1) When you wrapping your form component with reduxForm
you specify onSubmit
option. 1)使用
reduxForm
包装表单组件时,请指定onSubmit
选项。
class myForm extends React.Component {} export default reduxForm({ form: 'myForm', onSubmit: function(values) { console.log('hello', values) } })(myForm);
2) This function is available inside component as this.props.handleSubmit
so you still can write your own submit handler and call this.props.handleSubmit
inside it. 2)该函数在组件内部作为
this.props.handleSubmit
因此您仍然可以编写自己的提交处理程序并在其中调用this.props.handleSubmit
。
class myForm extends React.Component { myOwnSubmitHanlder() { console.log('hello', this.state); this.props.handleSubmit() } render() { return ( <form onSubmit={this.myOwnSubmitHanlder}></form> ) } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.