【问题标题】:How do you create multiple forms on the same page with redux-forms v6?如何使用 redux-forms v6 在同一页面上创建多个表单?
【发布时间】:2016-09-03 17:58:15
【问题描述】:

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

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

(TodoForm1、TodoForm2、TodoForm3 都可以有一个唯一的 'title' 字段,而不是包含 'title1'、'title2'、'title3' 字段的 TodoForm)。

我尝试访问 TodoForm 的道具,以便我可以将每个表单的键设置为组件的唯一 id,但似乎组件并没有那么早收到道具。

我还尝试创建一个立即调用的函数,它会输出一个随机数,并使用该数字作为表单的名称,但这也没有用。

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

这是应用程序、控制台和 redux 开发工具的图片。有 3 个“待办事项”,但只有一个表单将它们全部连接起来,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);

【问题讨论】:

    标签: javascript forms reactjs redux redux-form


    【解决方案1】:

    为了给你的表单提供动态键,你应该在你的 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 函数调用)

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

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2020-07-20
      • 2023-04-02
      • 2019-09-05
      • 1970-01-01
      相关资源
      最近更新 更多