【问题标题】:how to get <Field> values in redux-form 7.0.0如何在 redux-form 7.0.0 中获取 <Field> 值
【发布时间】:2017-07-17 11:55:48
【问题描述】:

class Form extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    const { Add, noteList } = this.props;
    Add('this is title's value' , 'this is content's value');
  }

  render() {
    const { handleSubmit, noteList: { list } } = this.props;
    return (
      <form onSubmit={handleSubmit(this.handleClick)}>
        <div>
          <Field className="title" name="title" component="input" type="text" />
        </div>
        <div>
          <Field className="content" name="content" component="textarea" />
        </div>
        <div>
          <button type="submit" onClick={(e) => { list ? this.handleClick : e.preventDefault(); }}>add</button>
        </div>
      </form>
    );
  }
}

当我单击按钮时,我希望将这两个值放入一个函数中添加为两个参数以执行异步操作,我该怎么办,请帮助我

【问题讨论】:

    标签: javascript reactjs redux redux-form


    【解决方案1】:

    handleClick函数中的字段值可以作为对象获取onSubmit

    class Form extends React.Component {
      constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
      }
    
      handleClick(values) {
        const { Add, noteList } = this.props;
        Add(values.title , values.content);
      }
    
      render() {
        const { handleSubmit, noteList: { list } } = this.props;
        return (
          <form onSubmit={handleSubmit(this.handleClick)}>
            <div>
              <Field className="title" name="title" component="input" type="text" />
            </div>
            <div>
              <Field className="content" name="content" component="textarea" />
            </div>
            <div>
              <button type="submit" onClick={(e) => { list ? this.handleClick : e.preventDefault(); }}>add</button>
            </div>
          </form>
        );
      }
    }
    

    【讨论】:

    • 非常感谢!! .我可以继续做我的表单演示
    • 很高兴有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2019-01-15
    • 2021-10-13
    • 1970-01-01
    • 2021-06-28
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多