简体   繁体   English

如何将道具传递给模态

[英]How to pass props to a modal

I have a shopping application where I map over some products and render them on screen. 我有一个购物应用程序,可以在其中映射一些产品并将其呈现在屏幕上。 users can increase/decrease quantity. 用户可以增加/减少数量。 when the quantity gets to 1 and the user hits decrease, some middleware jumps in and asks if they are sure they want to remove it from the basket. 当数量达到1且用户点击量减少时,一些中间件跳入并询问他们是否确定要从购物篮中删除它。 if they click no then it will close the modal and leave it in the basket. 如果他们单击否,它将关闭模式并保留在购物篮中。 if they click yes, it will close the modal and remove it from basket 如果他们单击是,它将关闭模式并将其从购物篮中删除

how can I pass props to the modal to make sure I delete the correct product? 如何将道具传递给模态,以确保删除正确的产品?

so far I have this. 到目前为止,我有这个。 all the functionality is there and the ability to remove is there. 所有功能都在那里,并且可以删除。 I'm just not sure how to pass the particular product to the modal? 我只是不确定如何将特定产品传递给模态? the reason that the increment/decrement work is because they are part of the map that maps over each product. 进行增减工作的原因是它们是映射到每个产品上的map一部分。 obviously when the modal loads, it is not part of the map. 显然,当模态负载时,它不是地图的一部分。 I have tried including it in the map but obviously this renders a modal for each product which isn't useful 我尝试将其包含在地图中,但显然这为每个产品提供了一个模态,这是没有用的

<h4> Bag </h4>
<Modal />
{bag.products.map((product, index) => (
  <div key={index}>
    <div>{product.name}</div>
    <div>£{product.price}</div>
    <div>
      <span> Quantity:{product.quantity} </span>
      <button onClick={() => this.props.incrementQuantity(product, product.quantity += 1)}> + </button>
      <button onClick={() => this.props.decrementQuantity(product)}> - </button>
    </div>
  </div>
))}

any ideas? 有任何想法吗?

I faced a similar scenario recently. 我最近也遇到过类似的情况。 I managed it using redux/global state as I had to keep track of many modals. 我使用redux / global状态来管理它,因为我必须跟踪许多模态。 Similar approach with local state 与当地州类似的方法

//****************************************************************************/

constructor(props) {

    super(props)


    this.state = {
      isModalOpen: false,
      modalProduct: undefined,
    }
}

//****************************************************************************/

render() {

    return (
        <h4> Bag </h4>
        {this.state.isModalOpen & (
          <Modal 
            modalProduct={this.state.modalProduct}
            closeModal={() => this.setState({ isModalOpen: false, modalProduct: undefined})
            deleteProduct={ ... }
          />
        )

        {bag.products.map((product, index) => (
        <div key={index}>
            <div>{product.name}</div>
            <div>£{product.price}</div>
            <div>
            <span> Quantity:{product.quantity} </span>
            <button onClick={() => this.props.incrementQuantity(product, product.quantity += 1)}> + </button>
            <button onClick={() => this.decrementQuantity(product)}> - </button> // <----
            </div>
        </div>
        ))}
    )
}

//****************************************************************************/

decrementQuantity(product) {

    if(product.quantity === 1) {
        this.setState({ isModalOpen: true, modalProduct: product })
    } else {
        this.props.decrementQuantity(product)
    }
}

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

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