【发布时间】:2017-12-26 23:31:09
【问题描述】:
我正在使用这个反应模式插件:https://github.com/reactjs/react-modal 我需要在页面加载时在模式中显示一组对象。当第一项显示用户单击按钮时,模态的 isOpen 属性设置为 false。每个项目都有一个道具 showModal 将值提供给模态的 isOpen。随着用户不断单击,我不断将当前对象的值设置为 false,然后为下一个对象设置为 true。 这一切都很好,但问题是覆盖和对话窗口留在屏幕上,只有模式中的内容被更新。我希望模态完全关闭并打开以显示数组中下一个对象的内容。我不得不将我的代码剥离为下面的简化版本:
class ProductsModal extends React.Component {
constructor(props) {
super(props);
this.remindMeHandler = this.remindMeHandler.bind(this);
this.state = {
products: [],
modalId: 0
};
}
showModals() {
let products = this.state.products;
//A different function saves the array of product objects in the state so
//I can show them one by one
let currentProduct = products[this.state.popUpId];
if (products.length > 0) {
return <ProductItemModal
product={currentProduct}
showNextPopUp={() => this.showNextPopUp(currentProduct.productId)}
showPopUp={currentProduct['showModal']}
/>;
//showModal is a boolean for each product that sets the value of isOpen
}
}
showNextPopUp() {
//All this does is sets the "showModal" property to false for current
//product and sets it to true for next product so it shows in the Modal
}
render() {
return(
<div>
{this.showModals()}
</div>
);
}
}
class ProductItemModal extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<Modal
isOpen={this.props.showModal}
contentLabel="Product"
id={this.props.product.productId}
>
<div>
Product Data......
</div>
</Modal>
);
}
}
【问题讨论】:
-
您的问题不清楚。你能解释一下你想要更好地完成什么吗?
-
@AlexanderHiggins 好的,所以你知道模态如何打开并且有一个覆盖?我希望每次将 isOpen 设置为 false 时关闭它,因为我要通过产品。现在它没有发生,当我浏览产品时,带有覆盖的 Modal 保持打开状态。有意义吗?
-
您的模态框
isOpen有一个showModal的道具,但在您的ProductItemModal中,我根本看不到您设置 showModal。你的意思是showPopUp是showModal? -
@Simon 来自 this.props.showModal,在父组件 showPopUp={currentProduct['showModal']} 是从父组件发送的。所有这些状态变化都在 showNextPopUp() 中处理
-
我已经对 isOpen 的值进行了故障排除,那里一切正常。我想我还需要在这里做点别的事情。模态打开正常,当最后一个产品的 showModal 属性设置为 false 时,它会关闭。但在这两者之间它保持打开状态,只是刷新产品内容。
标签: javascript reactjs react-modal