简体   繁体   English

React:如何仅显示该特定 div 的模态弹出窗口

[英]React : How to display a modal popup only for that specific div

To make it more clear , basically what I'm trying to do is like Amazon为了更清楚,基本上我想做的就像亚马逊一样

There will be a bunch of products and once you click on the product ,Only that product and its details will be displayed on the Popup Modal.将有一堆产品,一旦您单击产品,弹出模式中只会显示该产品及其详细信息。

In my case , I have stored 3 data in an Array, I have mapped it out Which creates 3div and 3Modal Popup with buttons in each.在我的例子中,我已经在一个数组中存储了 3 个数据,我已经将它映射出来,它创建了 3div 和 3Modal Popup,每个都有按钮。

Once I click on the button of 1st div , I want the modal to be open for that first div only.单击 1st div 的按钮后,我希望该模式仅针对第一个 div 打开。

But right now when I click on the button all 3 Modal Popup.但是现在当我点击所有 3 模态弹出按钮时。

I'm new to React , I can do this same thing in JQuery and Javascript, But I'm not able to achieve this in React.我是 React 的新手,我可以在 JQuery 和 Javascript 中做同样的事情,但我无法在 React 中实现这一点。

Or is there a better approach to this ?或者有更好的方法吗? Like rather than looping and creating 3modal popup Can we create just one modal popup, That will display the data of the particular div of which button is being clicked?就像而不是循环和创建 3modal popup 我们可以只创建一个 modal popup,这将显示正在单击的按钮的特定 div 的数据吗?

My Current Code:我当前的代码:

App.js , Where i have created the Array App.js ,我在哪里创建了数组

在此处输入图片说明

Product.Js Where its being mapped out into 3div and also has the modal popup Product.Js 它被映射到 3div 并且还有模态弹出窗口

在此处输入图片说明

Let me know if you guys need more details如果你们需要更多细节,请告诉我

Thank you guys.谢谢你们。

3 Div that is being dunamically created with the data from array 3 用数组中的数据动态创建的 Div 在此处输入图片说明

But when i Click on any button , popup for all div pops up , Thats the issue但是当我单击任何按钮时,所有 div 的弹出窗口都会弹出,这就是问题所在

Of course, all modal will pop up at the same time.当然,所有模态都会同时弹出。 All modal using exactly same state which is this.state.showModal .所有模式都使用完全相同的状态,即this.state.showModal Once it gets true then all will just pop up.一旦它成为true那么一切都会弹出。 If you still like to have 3 modals like that.如果你仍然喜欢有 3 个这样的模态。 I suggest you to make the value of showModal state with JSON value.我建议您将showModal状态的值设为 JSON 值。 Maybe something like this:也许是这样的:

state = {
    showModal: {}
}

then for getModal() function:然后对于getModal()函数:

getModal = value => {//still using variable from `data.id`
    let key_to_update = {};
    key_to_update[value] = true;
    this.setState( {
        showModal: Object.assign( {}, this.state.showModal, key_to_update )
    } );
}



Then for the <Modal/> should looks like this:然后对于<Modal/>应该是这样的:

<Modal show={this.state.showModal[data.id]} onClick={()=> this.hideModal(data.id)}/>



To hide the modal you can do opposite of getModal() as follow:要隐藏模式,您可以执行与getModal()相反的getModal() ,如下所示:

hideModal = value => {//still using variable from `data.id`
    let key_to_update = {};
    key_to_update[value] = false;//Just different on this
    this.setState( {
        showModal: Object.assign( {}, this.state.showModal, key_to_update )
    } );
}

If you still interested and have a problem to implement it, I can help you create a working demo.如果您仍然有兴趣并在实现它时遇到问题,我可以帮助您创建一个工作演示。 Because I am not really test the code, just make it based on my experience and quick analysis.因为我不是真正测试代码,只是根据我的经验和快速分析来制作它。 However, personally, I like to have a single Modal for this kind of case.但是,就我个人而言,我喜欢为这种情况使用一个 Modal。 Just set a single "state" of "Product detail" then read that "state" from single Modal then show it at the same time.只需设置“产品详细信息”的单个“状态”,然后从单个 Modal 中读取该“状态”,然后同时显示它。

==== DEMO: MULTIPLE MODAL ELEMENT TECHNIQUE ===== ==== 演示:多模态元素技术 =====

Just like your comment, because you only need to show single modal at a time, then it will be much easier.就像您的评论一样,因为您一次只需要显示一个模态,那么它会容易得多。 We don't need to have multiple true/false condition like above.我们不需要像上面那样有多个真/假条件。 We can just use data.id as the true/false check to the showModal state like follow:我们可以使用data.id作为对showModal状态的真/假检查,如下所示:

class Product extends Component {
  state = {
    showModal: 0
  };

  getModal = value => {
    this.setState({ showModal: value });
  };

  hideModal = value => {
    this.setState({ showModal: 0 });
  };

  render() {
    return (
      <div className="container">
        {this.props.data.map((data, key) => (
          <div key={key} className="small">
            <p>Namsse: {data.name}</p>

            <button onClick={() => this.getModal(data.id)}>Popup</button>

            <Modal
              show={this.state.showModal === data.id}
              onHide={() => this.hideModal(data.id)}
              name={data.name}
            />
          </div>
        ))}
      </div>
    );
  }
}

Working Demo: https://codesandbox.io/s/pkjvy72mw0工作演示: https : //codesandbox.io/s/pkjvy72mw0



===DEMO: SINGLE MODAL ELEMENT TECHNIQUE=== ===演示:单模态元素技术===

You can also have only single <Modal/> element just like below:您也可以只有一个<Modal/>元素,如下所示:

class Product extends Component {
  state = {
    showModal: false,
    dataModal: {
      name: ""
    }
  };

  getModal = data => {
    this.setState({ showModal: true, dataModal: data });
  };

  hideModal = () => {
    this.setState({ showModal: false });
  };

  render() {
    return (
      <div className="container">
        {this.props.data.map((data, key) => (
          <div key={key} className="small">
            <p>Namsse: {data.name}</p>

            <button onClick={() => this.getModal(data)}>Popup</button>
          </div>
        ))}

        <Modal
          show={this.state.showModal}
          onHide={this.hideModal}
          name={this.state.dataModal.name}
        />
      </div>
    );
  }
}

Working demo: https://codesandbox.io/s/53x7m726xk工作演示: https : //codesandbox.io/s/53x7m726xk

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

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