我有一个简单的待办事项应用程序,其中我的redux商店包含一系列' todos'。我的Todo'组件映射到每个&touto'在商店里,呈现一个“TodoForm”#39;使用redux-forms v6的组件。
就像现在一样,每个人都会这样做。共享相同的表单名称/密钥,因此每次我在'标题中输入内容Field,它改变了标题'每个待办事项。我通过使用唯一的字段名称找到了一个解决方法,但我担心随着应用程序的增长,它会使事情变得复杂,并且更愿意使用独特的表单名称,因此每个字段都可以具有相同的名称而不会干扰其他字段表格
(TodoForm1,TodoForm2,TodoForm3都可以拥有一个独特的标题' Field而非TodoForm包含' title1',' title2',' title3& #39;字段)。
我尝试访问TodoForm的道具,这样我就可以将每个表单的键设置为组件的唯一ID,但看起来该组件并不像早期那样接收道具。
我还尝试制作一个立即调用的函数,它会吐出一个随机数,并使用该数字作为表单的名称,但这也没有用。
如何通过我的所有待办事项映射并使用唯一的表单键呈现v6 redux-form?
这是app,console和redux devtools的图片。虽然每个表单键应该是在一个立即调用的函数中随机生成的,但是只有一个表单可以连接它们todo-926。 / p>
HomePageMainSection.index.js
renderTodos(todo) {
if (!todo) {
return <div>No Todos</div>;
}
return (
<div key={todo.get('id')}>
<Todo
todo={todo}
updateTodo={this.props.updateTodo}
deleteTodo={this.props.deleteTodo}
/>
</div>
);
}
render() {
if (!this.props.todos) {
return <div>No Todos</div>;
}
return (
<div className={styles.homePageMainSection}>
<h1>Hey I'm the Main Section</h1>
<div>
{this.props.todos.get('todos').map(this.renderTodos)}
</div>
</div>
);
}
Todo.index.js:
renderTodo() {
if (this.state.editMode) {
return (
<TodoForm
todo={this.props.todo} changeTodoEditMode={this.changeTodoEditMode}
updateTodo={this.props.updateTodo}
/>
);
}
return (
<div className={styles.Todo} onClick={this.changeTodoEditMode}>
<div className="card card-block">
<h4 className="card-title">{this.props.todo.get('author')}</h4>
<p className="card-text">{this.props.todo.get('title')}</p>
<i
className={`${styles.deleteIcon} btn btn-danger fa fa-times`}
onClick={this.deleteTodo}
></i>
</div>
</div>
);
}
render() {
return (
<div className="col-xs-6 col-sm-4">
{this.renderTodo()}
</div>
);
}
TodoForm.index.js:
class TodoForm extends React.Component { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
this._handleSubmit = this._handleSubmit.bind(this);
}
_handleSubmit(formData) {
console.log('');
console.log('OG: ', this.props.todo)
console.log('formData: ', formData);
const data = this.props.todo.update('title', formData.get('title'));
console.log('data: ', data);
console.log('');
// this.props.updateTodo(data);
}
render() {
const { handleSubmit, pristine, submitting } = this.props;
return (
<form className={`${styles.todoForm} card`} onSubmit={handleSubmit(this._handleSubmit)}>
<div className="card-block">
<label htmlFor="title">{this.props.todo.get('title')}</label>
<div className={'form-group'}>
<Field name={`title`} component="input" type="text" placeholder="Enter new title" className="form-control" />
</div>
</div>
<div className="card-block btn-group" role="group">
<button
className="btn btn-success"
type="submit"
disabled={pristine || submitting}
>
Submit
</button>
<button
className="btn btn-danger fa fa-times"
onClick={this.props.changeTodoEditMode}
>
</button>
</div>
</form>
);
}
}
const randomNum = (() => {
const thing = Math.floor(Math.random() * 1000) + 1;
console.log('thing: ', thing);
console.log('notThing: ', TodoForm.props);
return thing;
})();
export default reduxForm({
form: `todo-${randomNum}`,
})(TodoForm);
答案 0 :(得分:18)
要为表单提供动态密钥,您应该在 TodoForm 组件上使用 form 属性:
renderTodo() {
if (this.state.editMode) {
return (
<TodoForm
form={'todo-' + this.props.todo.id}
todo={this.props.todo} changeTodoEditMode={this.changeTodoEditMode}
updateTodo={this.props.updateTodo}
/>
);
}
[...]
}
(而不是 this.props.todo.id 可能是您的 randomNum 函数调用)