简体   繁体   中英

how to get <Field> values in redux-form 7.0.0

 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> ); } } 

when i click the button, i hope to get these two values into a function Add as two arguments to do something async, what should i do , help me please

The field values in handleClick function can can be obtained onSubmit as an object.

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>
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM