带有mapstatetoprops的redux-form没有触发on

时间:2018-04-18 14:21:15

标签: javascript reactjs redux react-redux redux-form

我正在使用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)
);

0 个答案:

没有答案