我正在使用redux-form并在后台发出get请求以从api中获取类别列表。当我连接mapStateToProps时,我必须做一些“hacky'获取render方法以确认我所服务的类别的东西。但是现在我已经正确地渲染了类别,我的onSubmit函数没有被调用。下面是我的表单,然后是我的导出语句,就像它当前一样,然后是实际工作时的状态,即提交但没有连接mapStateToProps。我还根据我在此处阅读的initial values with redux-form
将我的导出声明更改为现在的状态renderCategories(field) {
return (
<div>
<select>
<option value=''>Select A category</option>
{_.map(field.categories, category => {
return (<option value={category.id} key={category.id}>{category.name}</option>);
})}
</select>
</div>
);
}
onSubmit(values) {
console.log('we made it to onSubmit: ', values)
this.props.createContact(values);
}
render(){
const { handleSubmit, categories } = this.props;
if(!categories.length) {
return <img src={logo} className="App-logo" alt="logo" />
}
return (
<div>
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<Field
label='First Name'
name='firstName'
component={this.renderField}
/>
<Field
label='Last Name'
name='lastName'
component={this.renderField}
/>
<Field
label='Email'
name='emailAddresses'
component={this.renderField}
/>
<Field
label='Categories'
name='categories'
props={{categories}}
component={this.renderCategories}
/>
<button type='submit' className='btn btn-primary'>CREATE CONTACT</button>
</form>
</div>
);
}
当前导出声明,没有工作onSubmit
export default connect(mapStateToProps, {createContact, fetchCategories})(
reduxForm({
validate,
form: 'NewContactForm',
})(NewContact));
旧的导出语句,没有mapStateToProps,但有工作提交
export default reduxForm({
validate,
form: 'NewContactForm'
})(
connect(null, { createContact })(NewContact)
);