简体   繁体   English

反应事件处理程序按钮单击不适用于首次单击

[英]React Event Handler Button Click Not Working on First Click

I am making the Recipe Box project from Free Code Camp. 我正在Free Code Camp制作Recipe Box项目。 I have an event handler that is supposed to send an object that contains an array property made up of ingredients up to a parent component which will display the items in the array. 我有一个事件处理程序,该事件处理程序应该发送一个对象,该对象包含由成分组成的数组属性,直到父组件,该父组件将在数组中显示项目。 The problem is, when I first click the button that fires the handler, it sends an empty array even though there are ingredients entered by the user, and the second time I click it, it sends the ingredients from the previous button click, and it goes on like this every time the button is clicked. 问题是,当我第一次单击触发处理程序的按钮时,即使用户输入了成分,它也会发送一个空数组,而当我第二次单击它时,它将发送上次单击按钮时的成分,并且每次单击该按钮时都将继续这样。 How can I fix this? 我怎样才能解决这个问题?

Method in question: 有问题的方法:

  handleSubmit() {
    let ingredientsArrayUpdater = (ingredient) => {
      this.setState(prevState => ({
        ingredientsArr: [
          ...prevState.ingredientsArr,
          ingredient
        ]
      }))
    }

    let splitUserInput = this.state.ingredients.split(',');

    splitUserInput.map(ingredient => {
      return(
      ingredientsArrayUpdater(ingredient.trim())
    )
    });

    let recipeObject = {
      recipeName: this.state.recipe,
      ingredientList: this.state.ingredientsArr,
      idNumber: Math.floor((Math.random() * 100000000) + 1)
    }
    this.props.addRecipe(recipeObject);
  }

Code for button that triggers event handler: 触发事件处理程序的按钮的代码:

 <button onClick={e => {this.handleSubmit()}}
               className="btn btn-outline-success btn-sm">
               Add Recipe
              <i className="fas fa-plus"></i>
 </button>

Here is the github repository to see all the components. 这是查看所有组件的github存储库。 Index.js is the parent component. Index.js是父组件。

https://github.com/EsTrevino/recipe-box-project/tree/master/src https://github.com/EsTrevino/recipe-box-project/tree/master/src

First, you aren't updating the state when you think you are. 首先,您不会以自己认为的状态更新状态。 You also aren't waiting for the updated state, after you call setState . 在调用setState之后,您也不必等待更新的状态。 I'm not sure about all that you're trying to achieve in this method, but a start is: 我不确定您要使用此方法实现的所有功能,但是一个开始是:

handleSubmit() {
  let splitUserInput = this.state.ingredients.split(',');
  let newArray = this.state.ingredientsArr.concat(splitUserInput.map(a => a.trim()));

  this.setState({ingredientsArr: newArray});

  let recipeObject = {
    recipeName: this.state.recipe,
    ingredientList: newArray,
    idNumber: Math.floor((Math.random() * 100000000) + 1)
  }
  this.props.addRecipe(recipeObject);
}

That's "cheating" using what we know the state will be updated to. 使用我们知道的状态将被更新为“欺骗”。 You can also use a callback for setState to be more unidirectional. 您也可以使用回调使setState更具单向性。

handleSubmit() {
  let splitUserInput = this.state.ingredients.split(',');
  let newArray = this.state.ingredientsArr.concat(splitUserInput.map(a => a.trim()));

  this.setState({ingredientsArr: newArray}, () => {
    let recipeObject = {
      recipeName: this.state.recipe,
      ingredientList: newArray,
      idNumber: Math.floor((Math.random() * 100000000) + 1)
    }
    this.props.addRecipe(recipeObject);
  });
}

Same difference, IMO. IMO,同样的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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