我一直在尝试处理 redux-form 和 immutablejs,我认为我已经掌握了大部分内容。但是,我知道我至少错过了一步。并非我希望传递给表单组件的所有道具都被传递给它。例如,formKey
是undefined
。当我尝试使用this.props.resetForm
. 该功能存在,但它似乎没有做任何事情。表单输入保持更改并且pristine
为假。这是我的表单和根减速器:
import React, {Component} from 'react';
import {reduxForm} from 'redux-form';
export const fields = [ 'firstName', 'lastName', 'email' ]
class ContactForm extends Component {
render() {
const {fields: {firstName, lastName, email}} = this.props;
const handleSubmit = function (e) {
e.preventDefault()
console.log('firstName', firstName.value)
}
const reset = function () {
console.log('before', this.props)
this.props.resetForm()
console.log('after', this.props)
}.bind(this)
return (
<form onSubmit={handleSubmit}>
<div className="form-group">
<label>First Name</label>
<input type="text" className="form-control" placeholder="First Name" {...firstName}/>
</div>
<div className="form-group">
<label>Last Name</label>
<input type="text" className="form-control" placeholder="Last Name" {...lastName}/>
</div>
<div className="form-group">
<label>Email</label>
<input type="email" className="form-control" placeholder="Email" {...email}/>
</div>
<button type="submit" className="btn btn-primary">Submit</button>
<br/><br/>
<button type="button" className="btn btn-primary" onClick={reset}>Reset</button>
</form>
);
}
}
const ContactFormContainer = reduxForm({
form: 'contact',
fields,
getFormState: (state, reduxMountPoint) => state.get(reduxMountPoint).toJS()
},
function (state, ownProps) {
const contact = state.get('contacts').get(ownProps.contactId);
return {
initialValues: contact.toJS()
}
}
)(ContactForm);
export default ContactFormContainer;
.
import Immutable, {Map, fromJS} from 'immutable'
import {reducer as formReducer} from 'redux-form';
import {combineReducers} from 'redux-immutable';
import {createStore} from 'redux';
import contacts from './contacts/reducer'
const rootReducer = combineReducers({
form: (state = Immutable.fromJS({}), action) => Immutable.fromJS(formReducer(state.toJS(), action)),
contacts
});
const initialState = Map();
export default createStore(rootReducer, initialState);