简体   繁体   English

如何将一个组件数据设置到另一个组件

[英]How to set one component data into other component

I have 2 component.我有 2 个组件。 one is header component and another is home component.一个是 header 组件,另一个是 home 组件。

I have create the cart on header component.我已经在 header 组件上创建了购物车。 But in home component when I click on addToCart button the value should be changed on cart.但是在主组件中,当我单击 addToCart 按钮时,应该在购物车上更改该值。

header.jsx header.jsx

const Header = () => {
  const handleSelect = (e) => {
    console.log(e);

  }
    return (
      <div className="container">
        <div className="filelist">
          <div className="add_to_cart fa fa-shopping-cart fa-2x"><span className="item_in_cart">{}</span></div>
        </div>
        </div>
    )
}
export default Header;

I want to write the logic in home component like when we click addToCart that value should displayed in cart.我想在 home 组件中编写逻辑,例如当我们单击 addToCart 时该值应显示在购物车中。 But the cart logic I have written in header component.但是我在 header 组件中编写了购物车逻辑。 home.jsx主页.jsx

const DisplayDataAtHome = () => {
   const json_data=[{
    "merchant_id": 1,
    "medium_egg": 104,
    "small_egg": 100,
    "desi_egg": 110,
    "d_keshor_egg": 98
  }, {
    "merchant_id": 2,
    "big_egg": 90,
    "medium_egg": 104,
    "desi_egg": 112,
    "d_keshor_egg": 100
  }]
  const AddToCart=(e)=>{
    alert(e.target.value)
    var element=document.getElementsByClassName('addToCart');
    element[0].classList.add("hideIt");
  }
    return (
      <>
      <Header />
      <table className="outerdiv" style={{align:'center'}}>{
      json_data.map((val, i)=>(
        <>
        {
            val['big_egg']  &&  
            <div className="innerDiv">
                
                   <img src={bigegg}></img>
                   <p align="left">Big Egg</p>
                   <p align="left">weight: 500gm.</p>
                   <p align="left">{"₹"+val.big_egg+"/-"}<button className="addToCart" onClick={e=>AddToCart(e)}>AddToCart</button></p>
                </div>
        }
        {
            val['desi_egg'] && 
            <div className="innerDiv">
                
                    <img src={smallegg}></img>
                    <p align="left">Small Egg</p>
                    <p align="left">weight: 500gm.</p>
                    <p align="left">{"₹"+val.desi_egg+"/-"}<button>ADD</button></p>
                </div>
        }
        </>
    ))
      }
      </table>
      </>
    )
}
export default DisplayDataAtHome;

I want whenever I will click on addToCart button the cart value should be changed which is there in header component.我希望每当我单击 addToCart 按钮时,购物车值应该更改,它位于 header 组件中。 How can I solve this problem?我怎么解决这个问题?

You can pass one component data to another component like this.您可以像这样将一个组件数据传递给另一个组件。

In home.jsx: Ex: if we need to pass the total items count to the header, Keep the value in state (ex: cartTotal).在 home.jsx 中: 例如:如果我们需要将总项目数传递给 header,请保留 state 中的值(例如:cartTotal)。 When calling AddToCart, add to the 'cartTotal' state value.调用 AddToCart 时,添加到 'cartTotal' state 值。

Ex: In AddToCart():例如:在 AddToCart() 中:

AddToCart = () => {
    this.setState ({ cartTotal: cartTotal + 1 });
}

Then you need to pass the value as props from home.jsx to header.jsx like this:然后,您需要将值作为道具从 home.jsx 传递到 header.jsx,如下所示:

<Header cartTotal={this.state.cartTotal} />

In header.jsx display the total value like this:在 header.jsx 中显示总值如下:

<div className="add_to_cart fa fa-shopping-cart fa-2x">
    <span className="item_in_cart">
        {this.props.cartTotal}
    </span>
</div>

Similar to this, you can modify your code to match your actual logic, to pass the item list, exactly using the same logic using this example code.与此类似,您可以修改代码以匹配您的实际逻辑,以传递项目列表,完全使用与此示例代码相同的逻辑。

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

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