提交按钮需要2次点击才能在React中调用函数

时间:2018-11-20 18:52:03

标签: javascript reactjs if-statement state form-submit

当我单击“提交”按钮时,需要单击以正确启动该功能。在执行if语句之前,我没有任何问题。它是否具有某些事实,即我内部有2条if语句,每次单击后它仅检查1条if语句?

  onSubmit = (e) => {

const { firstName, lastName, email, eventDate, validation} = this.state

if (firstName, lastName, eventDate, email) {
  this.setState({validation: true})
  if (new RegExp(/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,15}/g).test(email)) {
    this.setState({validation: true})
  } else {
    this.setState({validation: false})
    this.setState({errorMessage: 'Please enter correct email adress'})
  }
} else {
  this.setState({validation: false});
  this.setState({errorMessage: 'Please fill in all fields properly'})
} 

if (validation) {

  const newEvent = {
    firstName: this.state.firstName,
    lastName: this.state.lastName,
    email: this.state.email,
    eventDate: this.state.eventDate
  }

  this.props.addEvent(newEvent);

  this.setState({
    firstName: '',
    lastName: '',
    email: '',
    eventDate: '',
    validation: false,
    errorMessage: ''
  });
}

e.preventDefault();
}

3 个答案:

答案 0 :(得分:0)

您也不需要彼此直接直接进行两个setState调用。将它们设置为一个setState:

this.setState({     验证:错误,     errorMessage:'请输入正确的电子邮件地址', )}

答案 1 :(得分:0)

我认为您在if(validation)语句中使用了过时的Validation值。您能尝试一下吗?

onSubmit = (e) => {

const { firstName, lastName, email, eventDate} = this.state

if (firstName, lastName, eventDate, email) {
  this.setState({validation: true})
  if (new RegExp(/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,15}/g).test(email)) {
    this.setState({validation: true})
  } else {
    this.setState({validation: false})
    this.setState({errorMessage: 'Please enter correct email adress'})
  }
} else {
  this.setState({validation: false});
  this.setState({errorMessage: 'Please fill in all fields properly'})
} 

const {validation} = this.state // read correct validation.. not something that was previously set

if (validation) {

  const newEvent = {
    firstName: this.state.firstName,
    lastName: this.state.lastName,
    email: this.state.email,
    eventDate: this.state.eventDate
  }

  this.props.addEvent(newEvent);

  this.setState({
    firstName: '',
    lastName: '',
    email: '',
    eventDate: '',
    validation: false,
    errorMessage: ''
  });
}

e.preventDefault();
}

答案 2 :(得分:0)

解决了这个问题,他们成功的关键是将我的if(validation)语句主体移到setState的回调函数。

这是工作代码:

  onSubmit = (e) => {
const { firstName, lastName, email, eventDate, validation} = this.state

if (firstName, lastName, eventDate, email) {
  this.setState({validation: true})
  if (new RegExp(/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,15}/g).test(email)) {
    this.setState({validation: true}, () => {
      const newEvent = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        eventDate: eventDate
      }
      this.props.addEvent(newEvent);

      this.setState({
        firstName: '',
        lastName: '',
        email: '',
        eventDate: '',
        validation: false,
        errorMessage: ''
      })
    })
  } else {
    this.setState({validation: false, errorMessage: 'Please enter correct email adress'})
  }
} else {
  this.setState({validation: false, errorMessage: 'Please fill in all fields properly'})
}
e.preventDefault();
}