我的表格有一个奇怪的问题。我可以使用两个按钮添加和删除字段,但删除按钮已停止工作。我可以看到,在点击静止时,“@@ redux-form / ARRAY_REMOVE”动作被触发,但该字段不会消失。我真的不知道它为什么停止工作,我试图撤消我之后写的所有代码,但是无法跟踪错误。将不胜感激任何意见。这是我的代码:
import React from 'react';
import TextField from 'material-ui/TextField';
import { Field, FieldArray, reduxForm} from 'redux-form';
import validate from './validate';
import injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin(); //Needed, otherwise an error message is shown in the console
const renderTextField = ({input, label, meta: {touched, error}, ...custom}) => (
<TextField
hintText={label}
floatingLabelText={label}
errorText={touched && error}
{...input}
{...custom}
/>
);
const renderUsers = ({fields, meta: { touched, error }}) => (
<div>
<div>
<button className="btn btn-primary"
type="button" onClick={() => fields.push({})}>
<span className="glyphicon glyphicon-plus-sign"/> Add User
</button>
{touched && error && <span>{error}</span>}
</div>
{fields.map((index) =>
<div key={index}>
<Field name={`${index}.firstName`} component={renderTextField} label="First Name"/>
<Field name={`${index}.lastName`} component={renderTextField} label="Last Name"/>
<Field name={`${index}.position`} component={renderTextField} label="Position"/>
<Field name={`${index}.email`} component={renderTextField} label="Email"/>
<button className="btn btn-xs btn-danger"
type="button"
title="Remove User"
onClick={() => fields.remove(index)}>
<span className="glyphicon glyphicon-minus-sign"/>
</button>
</div>
)}
</div>
);
const UserCreation = (props) => {
const { handleSubmit, pristine, reset, submitting} = props;
return (
<form onSubmit={handleSubmit}>
<FieldArray name="users" component={renderUsers}/>
<div>
<button className="btn btn-primary btn-success"
type="submit"
disabled={pristine || submitting}>
<span className="glyphicon glyphicon-send" />
Submit
</button>
{' '}
<button type="button"
className="btn btn-primary btn-danger"
disabled={ pristine || submitting}
onClick={reset}>
Cancel
</button>
</div>
</form>
);
}
export default reduxForm({
form: 'UserCreationForm',
validate
})(UserCreation);
当我尝试删除该字段时,以下是我的控制台的快照:
答案 0 :(得分:0)
我修好了。我在地图功能中添加了第二个键:
{fields.map((user, index) =>
<div key={index}>
<Field name={`${user}.firstName`} component={renderTextField} label="First Name"/>
<Field name={`${user}.lastName`} component={renderTextField} label="Last Name"/>
<Field name={`${user}.position`} component={renderTextField} label="Position"/>
<Field name={`${user}.email`} component={renderTextField} label="Email"/>