如何使用 redux-forms v6 在同一页面上创建多个表单?
How do you create multiple forms on the same page with redux-forms v6?
我有一个简单的待办事项应用程序,其中我的 redux 商店包含一个 'todos' 数组。我的 'Todo' 组件映射商店中的每个 'todo' 并呈现一个使用 redux-forms v6.
的 'TodoForm' 组件
就像现在一样,每个 'todo' 共享相同的形式 name/key,所以每次我在 'title' 字段中输入内容时,它都会更改 'title'每个待办事项。我通过使用唯一的字段名称找到了解决方法,但我担心随着应用程序的增长它会使事情变得过于复杂,并且更愿意使用唯一的表单名称以便每个字段都可以具有相同的名称而不会干扰其他表单
(TodoForm1、TodoForm2、TodoForm3 都可以有一个唯一的 'title' 字段,而不是包含 'title1'、'title2'、'title3' 字段的 TodoForm。
我尝试访问 TodoForm 的道具,这样我就可以将每个表单的键设置为组件的唯一 ID,但组件似乎并没有那么早接收道具。
我还尝试制作一个立即调用的函数,它会吐出一个随机数,并将该数字用作表单的名称,但这也没有用。
我怎样才能映射我的所有待办事项并呈现具有唯一表单键的 v6 redux 表单?
这是应用程序、控制台和 redux devtools 的图片。有 3 个 'todos',但只有一个表单将它们全部连接起来,todo-926,即使每个表单键应该在立即调用的函数中随机生成:
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);
要为您的表单提供动态键,您应该在 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 函数调用)
我有一个简单的待办事项应用程序,其中我的 redux 商店包含一个 'todos' 数组。我的 'Todo' 组件映射商店中的每个 'todo' 并呈现一个使用 redux-forms v6.
的 'TodoForm' 组件就像现在一样,每个 'todo' 共享相同的形式 name/key,所以每次我在 'title' 字段中输入内容时,它都会更改 'title'每个待办事项。我通过使用唯一的字段名称找到了解决方法,但我担心随着应用程序的增长它会使事情变得过于复杂,并且更愿意使用唯一的表单名称以便每个字段都可以具有相同的名称而不会干扰其他表单
(TodoForm1、TodoForm2、TodoForm3 都可以有一个唯一的 'title' 字段,而不是包含 'title1'、'title2'、'title3' 字段的 TodoForm。
我尝试访问 TodoForm 的道具,这样我就可以将每个表单的键设置为组件的唯一 ID,但组件似乎并没有那么早接收道具。
我还尝试制作一个立即调用的函数,它会吐出一个随机数,并将该数字用作表单的名称,但这也没有用。
我怎样才能映射我的所有待办事项并呈现具有唯一表单键的 v6 redux 表单?
这是应用程序、控制台和 redux devtools 的图片。有 3 个 'todos',但只有一个表单将它们全部连接起来,todo-926,即使每个表单键应该在立即调用的函数中随机生成:
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);
要为您的表单提供动态键,您应该在 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 函数调用)