在redux-form 6.0.5中,值没有传递给提交函数
Values not being passed to on submit function in redux-form 6.0.5
由于某种原因,当我使用匿名函数定义组件时,这些值从未传递给提交处理程序。我正在使用redux-form 6.0.5
我创建了下面这个简单的表单:
class TestForm extends Component {
constructor(props) {
super(props);
}
submit(e) {
console.log(e);
}
renderField(field) {
return (
<input {...field.input} type="text" className="form-control" placeholder={field.placeholder} />
);
}
render() {
const handleSubmit = this.props.handleSubmit;
return (
<form onSubmit={handleSubmit(this.submit)}>
<div className="form-group">
<label>Title</label>
<Field name="title"
placeholder="Title"
component={this.renderField} />
</div>
<div className="form-group">
<label>Category</label>
<Field name="category"
placeholder="Category"
component={category => (
<input type="text" className="form-control" {...category}/>
)
} />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
);
}
}
当调用submit(e)时,控制台显示标题字段的值,但不显示类别的值。
是否有什么问题与我的匿名函数类别字段,或者匿名函数不支持作为组件?
我实际上和你在同一门课上,想发表一下这个问题是什么。从V4到V6有一个突破性的变化,在课程中我们使用的是redux形式的V4。我喜欢学习最新的东西,所以我更新了所有东西,但这很痛苦,但我确实找到了让表单实际保存的解决方案。首先,你必须让{ connect }
和{ reduxForm }
分开使用。此外,没有更多的领域,现在有领域,所以{ reduxForm, Field }
这是V6。至于需要看到的工作代码。
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { reduxForm, Field } from 'redux-form';
import { createPost } from '../actions/index';
const renderInput = field => (
<div className="form-group">
<label>{field.label}</label>
<input className="form-control" {...field.input} type= {field.type}/>
</div>
);
const renderTextarea = field => (
<div className="form-group">
<label>{field.label}</label>
<textarea className="form-control" {...field.input}/>
</div>
);
class PostsNew extends Component{
render(){
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit(this.props.createPost)}>
<h3>Create A New Post</h3>
<Field
label="Title"
name="title"
type="text"
component={renderInput} />
<Field
label="Categories"
name="categories"
type="text"
component={renderInput}
/>
<Field
label="Content"
name="content"
component={renderTextarea}
/>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
);
}
}
export default reduxForm({
form: 'PostsNewForm',
})(connect(null,{ createPost })(PostsNew));
我希望这对你有帮助,如果不是你确切的问题,有一个迁移指南。v5-v6
相关文章:
- HTML表单提交时未执行外部函数
- Jquery函数在错误的时间提交
- 提交按钮通过JQuery和JavaScript函数所做的更改不会持续
- jQuery preventdefault提交表单没有'通过js函数提交表单时无法工作
- 使用一个Javascript函数提交多个表单可以在FF中工作,而不需要其他浏览器
- Ajax Post-Submit函数提交两次
- 如何使用 javascript 使用 angular 2 http post 函数提交表单
- Javascript 无法执行函数 - 提交和操作
- JavaScript 从函数提交表单
- 向Javascript函数提交表单字段值
- 如何停止AJAX函数提交
- 在yii中,我试图使用js函数提交
- 无法通过javascript函数提交表单
- 调用JavaScript函数提交表单和点击按钮
- 当表单通过load() ajax函数提交时,我如何清除文本框中的文本
- javascript的Onclick函数提交按钮不工作
- 如何在JSF中使用javascript函数提交表单,不带表单名称或id
- 使用单独的 Javascript 函数提交隐藏的PayPal表单
- 调用所有函数提交表单
- 在PHP中检测通过提交按钮或javascript提交函数提交表单的区别