简体   繁体   English

如何通过删除 function 作为 ReactJS 中的道具

[英]How to pass a delete function as props in ReactJS

I am building a recipe app and am having trouble with a delete function.我正在构建一个食谱应用程序,但在删除 function 时遇到问题。

How it works is you click an "Add Recipe" button and a modal pops up that lets you fill in some input fields for a new recipe.它的工作原理是单击“添加配方”按钮,然后弹出一个模式,让您填写一些新配方的输入字段。 Within that form is an ingredient field and an "Add Ingredient" button.在该表单中是一个成分字段和一个“添加成分”按钮。 You click "Add" and the ingredient is added to a list of ingredients.您单击“添加”,该成分将添加到成分列表中。 It behaves exactly like a todo list, but it is inside of a form component.它的行为与待办事项列表完全相同,但它位于表单组件内部。 I also want to be able to remove an ingredient.我还希望能够去除一种成分。

My app components are structured like this:我的应用程序组件的结构如下:

  • RecipeBook.js (parent) RecipeBook.js(父)
  • RecipeCardForm.js (child) RecipeCardForm.js(子)

This is the form component located in the parent component:这是位于组件中的表单组件:

<RecipeCardForm
handleRemoveIngredients={this.handleRemoveIngredients}
/>

Also in the parent component is the removeIngredient function:组件中还有removeIngredient function:

handleRemoveIngredient = id => {
  const filteredIngredientList =  this.state.ingredientList.filter(ingredient => id !== ingredient.id);
  this.setState({ingredientList: filteredIngredientList})
  }

In the child component, this is returned inside a <ul> which displays the ingredientsList:组件中,这在显示成分列表的<ul>中返回:

{this.props.ingredientList.map(ingredient => {

              return (
                <li key={ingredient.id}>
                    <span>{ingredient.amount}</span>
                    <span>{ingredient.ingredient}</span>
                    <span ><button type='button' className='btn btn-danger' 
                    onClick={ this.props.removeIngredient }>X</button></span>
                </li>
              )
            })}

Because of how it's structured, I cannot figure out how to pass the id along which would normally go in the delete function of the delete prop in the parent component.由于它的结构,我无法弄清楚如何在父组件中删除道具的删除 function 中传递通常 go 的 id。

Normally, I would do something like this:通常,我会做这样的事情:

this.state.items.map(item => {
<List
handleRemoveItem={() => this.handleRemoveItem(item.id)} />

But since nothing is being mapped in the parent component, I have no way to pass the id along.但是由于父组件中没有映射任何内容,因此我无法传递 id。

Because of this, when I log the id in the removeIngredient function, it comes up as undefined.因此,当我在removeIngredient function 中记录 id 时,它显示为未定义。 I know its there though.我知道它在那里。 When i log it in the add function, there is an id, and when I log it as I am mapping each ingredient in the child component, it is also there.当我在添加 function 中记录它时,有一个 id,当我在映射子组件中的每个成分时记录它时,它也在那里。 I can even access it in the onClick of the delete ingredient button:我什至可以在删除成分按钮的 onClick 中访问它:

<button 
 type='button' 
 className='btn btn-danger' 
 onClick={ () => console.log(ingredient.id)}>Delete</button>

That gives me the id too.这也给了我id。

I just cannot wrap my mind around how to pass it without mapping in the parent component but there is literally nothing to map in the parent component.我只是想不通如何在不映射父组件的情况下传递它,但父组件中的 map 实际上没有任何内容。 And of course passing "this.state.id" just gives me the initial state which is an empty string.当然,传递“this.state.id”只会给我一个空字符串的初始 state。

Any help would be much appreciated to help me understand how to do this.任何帮助将不胜感激,以帮助我了解如何做到这一点。 Thanks in advance!提前致谢!

I think the solution to your problem would be to pass the ID to the this.props.handleRemoveIngredient function like this:我认为解决您的问题的方法是将 ID 传递this.props.handleRemoveIngredient function,如下所示:

onClick={this.props.handleRemoveIngredient(ingredient.id)}

In your example you did not hand the function the ingredient id and did call the this.props.removeIngredient function which is not the correct name of the prop / function.在您的示例中,您没有将 function 交给成分 ID,而是调用了this.props.removeIngredient function,这不是道具/ ZC1C425268E68385D1AB4Z144F 的正确名称。

Nevermind, just figured it out.没关系,刚刚想通了。

The solution was indeed onClick={ () => this.props.removeIngredient(ingredient.id) }解决方案确实是onClick={ () => this.props.removeIngredient(ingredient.id) }

My issue was when I was passing the props to the child component handleRemoveIngredient={this.handleRemoveIngredient} I was passing it as a function with no arguments handleRemoveIngredient={() => this.handleRemoveIngredient()} and for some reason that was making the id come back undefined.我的问题是当我将道具传递给子组件handleRemoveIngredient={this.handleRemoveIngredient}我将它作为 function 传递,没有 arguments handleRemoveIngredient={() => this.handleRemoveIngredient()} id 回来未定义。

Thanks for the help!!谢谢您的帮助!!

{this.props.ingredientList.map(ingredient => {

          return (
            <li key={ingredient.id}>
                <span>{ingredient.amount}</span>
                <span>{ingredient.ingredient}</span>
                <span ><button type='button' className='btn btn-danger' 
                onClick={ () => this.props.removeIngredient(ingredient.id) }>X</button></span>
            </li>
          )
        })}

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

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