【问题标题】:Value not getting passed from child to parent component in React在 React 中,值没有从子组件传递到父组件
【发布时间】:2018-01-19 06:24:14
【问题描述】:

正如您在下面的两个组件中看到的那样,我想通过单击面板组件中的按钮来删除食谱(在应用程序组件中), 我在应用程序中有一个删除配方的方法,并将一个 prop(onclick) 发送到子面板组件。 Panel 然后从配方映射中获取索引,并在单击按钮后执行 handleDelet 方法将索引发送回父级。但不,这是行不通的!

    class App extends React.Component {

  state={
    addRecipe:{recipeName:"",ingredients:[]},
    recipes:[{recipeName:"Apple",ingredients:["apple","onion","spice"]},
            {recipeName:"Apple",ingredients:["apple","onion","spice"]},
            {recipeName:"Apple",ingredients:["apple","onion","spice"]}]
  }
handleDelete = (index) => {
  let recipes = this.state.recipes.slice();
  recipes.splice(index,1); //deleting the index value from recipe
  this.setState({recipes}) //setting the state to new value
  console.log(index,recipes)
}
  render() {
    return (
      <div className="container">
        <PanelComponent recipes={this.state.recipes} onClick={()=>this.handleDelete(index)}/>
        <ModalComponent />
      </div>
    );
  }
}

class PanelComponent extends React.Component {
  handleDelete = (index) => {
    this.props.onClick(index); //sending index to parent after click
    console.log(index)
  }
  render() {

    return (
      <PanelGroup accordion>
        {this.props.recipes.map( (recipe,index) => {
          return(

          <Panel eventKey={index} key={index}>
          <Panel.Heading>
            <Panel.Title toggle>{recipe.recipeName}</Panel.Title>
          </Panel.Heading>
          <Panel.Body collapsible>
            <ListGroup>
              {recipe.ingredients.map((ingredient)=>{
                return(<ListGroupItem>{ingredient}</ListGroupItem>);
              })} 
            </ListGroup>
            <Button bsStyle="danger" onClick={()=>this.handleDelete(index)}>Delete</Button>
              <EditModalComponent />
          </Panel.Body>
        </Panel>

          );

        })}


      </PanelGroup>
    );
  }
}

【问题讨论】:

  • 我认为你错误地将函数作为道具发送,试试这个 否则你正在sneding函数的结果作为道具而不是函数本身
  • @Sujit.Warrier 能否编辑并显示给我看。谢谢
  • @Sujit.Warrier 点击不更新后我现在正在获取索引值
  • 索引值在parent还是child?
  • @Sujit.Warrier 现在可以使用了,谢谢

标签: javascript reactjs


【解决方案1】:

您的代码中的实际错误是,在父级的 onClick 中使用箭头函数时,您传递了错误的参数,而不是 {()=&gt;this.handleDelete(index)} 您应该写的是 {(value)=&gt;this.handleDelete(value)},但是,这也不是必需的,您可以在 App 中简单地编写 {this.handleDelete},因为您的 handleDelete 函数已经绑定并且它接收来自子组件的值。

render() {
    return (
      <div className="container">
        <PanelComponent recipes={this.state.recipes} onClick={(value)=>this.handleDelete(value)}/>
        <ModalComponent />
      </div>
    );
  }

{()=&gt;this.handleDelete(index)}{(value)=&gt;this.handleDelete(value)} 的区别在于,在第一种情况下,您明确传递从 App 组件中的 map 函数获得的索引,而在第二种情况下,从子项传递的值执行this.props.onClick(value) 时的组件被提供给handleDelete 函数。

【讨论】:

    【解决方案2】:

    您错误地将函数作为道具发送。您将函数的结果作为道具而不是函数本身发送

      class App extends React.Component {
    
     state={
    addRecipe:{recipeName:"",ingredients:[]},
    recipes:[{recipeName:"Apple",ingredients:["apple","onion","spice"]},
            {recipeName:"Apple",ingredients:["apple","onion","spice"]},
            {recipeName:"Apple",ingredients:["apple","onion","spice"]}]
     }
    handleDelete = (index) => {
     let recipes = this.state.recipes.slice();
     recipes.splice(index,1); //deleting the index value from recipe
     this.setState({recipes}) //setting the state to new value
     console.log(index,recipes)
     }
     render() {
       return (
         <div className="container">
             //change here
           <PanelComponent recipes={this.state.recipes} onClick={this.handleDelete}/>
        <ModalComponent />
      </div>
      );
     }
     }
    

    【讨论】:

    • OP 没有发送函数的结果,它是一个箭头函数语法
    • 很好,但他也没有发送函数的引用
    • no 那不是错误,问题是{()=&gt;this.handleDelete(index)} 在此调用中,handleDelete 函数从上面的 map 函数中获取索引,而编写 {(value)=&gt;this.handleDelete(value)} 它获取执行时传递的值,将其传递给 handleDelete 函数
    • 是的,您的建议可以正常工作,但您对答案的解释不正确,重要的是要知道为什么您的方式有效,而不是其他人使用什么
    猜你喜欢
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多