[英]Why does my modal not hide in react when I change my state from true to false?
I have a modal component that is dependent on a state in a portfolio component that looks like this:我有一个模态组件,它依赖于投资组合组件中的状态,如下所示:
import React, { Component } from 'react';
import Modal from './Modal';
export default class Porfolio extends Component {
constructor(props) {
super(props);
this.state = {
modalIsOpen: false
};
}
render() {
const handleClose = () => {
console.log("Is this working?")
this.setState({ modalIsOpen: false })
};
let resumeData = this.props.resumeData;
return (
<section id="portfolio">
<div className="row">
<div className="twelve columns collapsed">
<h1>Check Out Some of My Works.</h1>
<div id="portfolio-wrapper" className="bgrid-quarters s-bgrid-thirds cf">
{
resumeData.portfolio && resumeData.portfolio.map((item) => {
return (
<div className="columns portfolio-item">
<div className="item-wrap">
<img src={`${item.imgurl}`} className="item-img" />
<div className="overlay" onClick={() => this.setState({ modalIsOpen: true })} >
{this.state.modalIsOpen ? <Modal item={item} handleClose={handleClose} /> : <div />}
<div className="portfolio-item-meta">
<h5>{item.name}</h5>
<p>{item.description}</p>
</div>
</div>
</div>
</div>
)
})
}
</div>
</div>
</div>
</section>
);
}
}
My modal component is coded like this:我的模态组件是这样编码的:
import React, { Component } from 'react';
export default class Modal extends Component {
render() {
let item = this.props.item;
let handleClose = this.props.handleClose;
let state = this.props.state;
return (
<div className="popup-modal">
<div className="modal-content">
<div className="description-box">
<button onClose={handleClose}>X</button>
<h4>Testing</h4>
<p>This is a test</p>
<a href="#">This is a clickable link</a>
</div>
</div>
</div >
)
}
}
My modal opens just fine when clicked, but on closing the test "x" button in my component, it seems to be running the function and I believe the state is being changed to false, but it is not hiding the modal.我的模态在单击时打开得很好,但是在我的组件中关闭测试“x”按钮时,它似乎正在运行该功能,我相信状态正在更改为 false,但它并没有隐藏模态。 Can anyone see if I'm missing anything?
谁能看看我是否遗漏了什么?
For the full github repo: https://github.com/WarriorofZarona/React-Portfolio-1对于完整的 github 存储库: https : //github.com/WarriorofZarona/React-Portfolio-1
I believe your close click event is bubbling.我相信您的关闭点击事件正在冒泡。
Your code when rendered will be something like this:呈现时您的代码将是这样的:
<div className="overlay" onClick={() => this.setState({ modalIsOpen: true })} > // Click handler one
this.state.modalIsOpen ?
<div className="popup-modal">
<div className="modal-content">
<div className="description-box">
<button onClick={handleClose}>X</button> // Click handler two
<h4>Testing</h4>
<p>This is a test</p>
<a href="#">This is a clickable link</a>
</div>
</div>
</div >
: <div />}
<div className="portfolio-item-meta">
<h5>{item.name}</h5>
<p>{item.description}</p>
</div>
</div>
The button
onClick will always trigger the div
onClick because the button
is a child of the div
. button
onClick 将始终触发div
onClick 因为该button
是div
的子项。 This means as soon as the modal is closed it will be re-opened.这意味着一旦模态关闭,它将重新打开。
To fix this, you need to call e.stopPropagation();
要解决此问题,您需要调用
e.stopPropagation();
from the inner click handler.从内部点击处理程序。
const handleClose = (e) => {
e.stopPropagation();
console.log("Is this working?")
this.setState({ modalIsOpen: false })
};
Here are a few running examples to illustrate.这里有几个运行示例来说明。
Does not work:不起作用:
class Example extends React.Component { constructor(props) { super(props); this.state = { open: false } } handleClick = (value) => { this.setState({ open: value }); } render() { console.log(this.state.open) return ( <div onClick={() => this.handleClick(false)}> <button onClick={() => this.handleClick(true)}>Click me</button> </div> ) } } ReactDOM.render(<Example/>,document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root" />
Does work:是否有效:
class Example extends React.Component { constructor(props) { super(props); this.state = { open: false } } handleClick = (value) => { this.setState({ open: value }); } render() { console.log(this.state.open) return ( <div onClick={() => this.handleClick(false)}> <button onClick={(e) => { e.stopPropagation(); this.handleClick(true); }} >Click me</button> </div> ) } } ReactDOM.render(<Example/>,document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.