[英]How to Double Click Toggle visibility of React Component?
Currently have a popup component showing up double click using the onDoubleClick()
handler.But I'd like to close that popup on double click of the popup component but I can't seem to get it to work.目前有一个弹出组件显示使用
onDoubleClick()
处理程序双击。但我想在双击弹出组件时关闭该弹出窗口,但我似乎无法让它工作。 Here is what I have been trying, the thought process was to just to set toggleModal to false and it should work.这是我一直在尝试的,思考过程只是将toggleModal设置为false,它应该可以工作。
const [selectedImageId, setSelectedImageId] = useState(-1);
const [toggleModal, setToggleModal] = useState(false);
const handleModalPopupOnClick = (id) => {
setSelectedImageId(id);
setToggleModal(true);
};
return (
<div>
{toggleModal && <PopupModal onDoubleClick={setToggleModal(false)}/>}
<div onDoubleClick{()=> handleModalPopupOnClick(image.id)>Open Popup</div>
</div>
)
Any ideas?有任何想法吗? Thank you for any suggestions or guidance.
感谢您的任何建议或指导。
The line线
{toggleModal && <PopupModal onDoubleClick={setToggleModal(false)}/>}
Is immediately calling setToggleModal
with an argument of false when the component is rendered, and I believe undefined
becomes the value of onDoubleClick
.渲染组件时立即使用参数 false 调用
setToggleModal
,我相信undefined
成为onDoubleClick
的值。 (Not 100% on if setState
has a return value or not) (如果
setState
有返回值,则不是 100%)
To fix your problem you should provided this as a prop:要解决您的问题,您应该将其作为道具提供:
{toggleModal && <PopupModal onDoubleClick={() => setToggleModal(false)}/>}
This is providing a function definition rather than calling the function.这是提供 function 定义,而不是调用 function。
Maybe PopupModal
is your custom component, so it doesn't provide onDoubleClick
event.也许
PopupModal
是您的自定义组件,因此它不提供onDoubleClick
事件。
onDoubleClick
in this line: onDoubleClick
在这一行:
{toggleModal && <PopupModal onDoubleClick={setToggleModal(false)}/>}
is just a prop.只是一个道具。 So you must call
props.onDoubleClick
in the handler of onDoubleClick
event inside the PopupModal
.因此,您必须在
PopupModal
内的onDoubleClick
事件的处理程序中调用props.onDoubleClick
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.