如何使用redux-forms v6在同一页面上创建多个表单?

Kyl*_*ong 11 javascript forms reactjs redux redux-form

我有一个简单的待办事项应用程序,其中我的redux商店包含一系列'todos'.我的'Todo'组件映射到商店中的每个'todo',并呈现使用redux-forms v6的'TodoForm'组件.

就像现在一样,每个'todo'都共享相同的表单名称/密钥,所以每次我在'title'字段中输入内容时,它都会改变每个待办事项的'标题'.我通过使用唯一的字段名称找到了一个解决方法,但我担心随着应用程序的增长,它会使事情变得复杂,并且更愿意使用唯一的表单名称,因此每个字段都可以具有相同的名称而不会干扰其他表单

(TodoForm1,TodoForm2,TodoForm3都可以有一个独特的'标题'字段,而不是包含'title1','title2','title3'字段的TodoForm.

我尝试访问TodoForm的道具,所以我可以将每个表单的键设置为组件的唯一ID,但似乎组件不会早期接收道具.

我也尝试制作一个立即调用的函数,它会吐出一个随机数,并使用该数字作为表单的名称,但这也无效.

如何通过我的所有待办事项映射并使用唯一的表单键呈现v6 redux-form?

这是app,console和redux devtools的图片.有3个'todos',但是只有一个表单连接它们todo-926,即使每个表单键应该是在一个立即调用的函数中随机生成的:

Todo Conundrums

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>
    );
  }
Run Code Online (Sandbox Code Playgroud)

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>
    );
  }
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

Mis*_*a K 19

要为表单提供动态密钥,您应该在TodoForm组件上使用表单属性:

renderTodo() {
    if (this.state.editMode) {
      return (
        <TodoForm
          form={'todo-' + this.props.todo.id}  
          todo={this.props.todo} changeTodoEditMode={this.changeTodoEditMode}
          updateTodo={this.props.updateTodo}
        />
      );
    }
 [...]
}
Run Code Online (Sandbox Code Playgroud)

(而不是this.props.todo.id可能是你的randomNum函数调用)

API参考:http://redux-form.com/6.0.2/docs/api/Props.md/